我想解析AWS GetMetricData API返回的以下JSON数组。它包含不同数组中的时间戳和值:
{
"Messages": [],
"MetricDataResults": [
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z",
"2021-12-28T01:51:00Z"
],
"StatusCode": "Complete",
"Values": [
5.0,
2.0,
2.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0
],
"Id": "groupDesiredCapacity",
"Label": "myRequestLabel"
},
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z",
"2021-12-28T01:51:00Z"
],
"StatusCode": "Complete",
"Values": [
5.0,
2.0,
2.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0
],
"Id": "groupInServiceCapacity",
"Label": "myRequestLabel"
},
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z"
],
"StatusCode": "Complete",
"Values": [
94.20519316022799,
97.325,
99.95833333333333,
99.94166666666666,
97.35833333333333,
62.375,
96.61666666666666,
0.3916666666666666,
0.3833525009583812,
0.391647084312451,
24.05873431223854,
60.84898585023583,
64.54059099015015,
25.07541792363206,
0.25,
0.2499958334027766,
0.28333333333333327,
0.4000066667777796,
58.31569473842103,
5.98135840794e-05
],
"Id": "cpuUtilization",
"Label": "myRequestLabel"
}
]
}
我将尝试将其转换为如下的表格:
Timestamp | Value | Id
2021-12-28T02:11:00Z | 5.0 | groupDesiredCapacity
我在谷歌上搜到了这个jq查询语句: .MetricDataResults[] | {t: .Timestamps[], v: .Values[],i: .Id}|[.t,.v,.i]|@csv
。但是它似乎是把值“乘以”而不是映射它们。
对此有些困惑,欢迎提供任何想法/帮助。
.Timestamps
和.Values
数组长度相同(否则,即如果一个比另一个短,transpose
将用null
值填充间隙)。如果在这种情况下,您想将输出行数减少到.Timestamps
数组中条目的(可能更小的)数量,请使用@peak的解决方案。 - pmf