我试图使用jq来组合两个数组并遇到一些麻烦。
我试图解析netdata(netdata.firehol.org)中的数据,而我感兴趣的json响应中的两个数据都是数组的一部分。第一个数组是第二个数组中数据点的标签。
示例输入
[
"time",
"guest_nice",
"guest",
"steal",
"softirq",
"irq",
"user",
"system",
"nice",
"iowait"
]
[
1460728600,
0,
0,
0,
0.45731,
0,
0.25108,
11.74702,
48.22465,
0
]
输入
如果您想自己抓取新数据进行测试,可以使用以下内容:
curl -s -X GET --header 'Accept: application/json'
'http://netdata.firehol.org/api/v1/data?chart=system.cpu&after=-10&before=0&points=1&group=average&format=json&options=seconds%2Cjsonwrap' | jq '.result.labels, .result.data[]'
我试图使用map()以及尝试将变量分配给两个数组,然后将这些对象打印出来,但一直都不成功(下图)。
代码
| jq '.result.labels as $labels | .result.data[] as $data | .result.data[] | Label: $labels[.], data: $data[.]}'
我提前了解任何人的洞察力,因为我有点陷入困境,并且宁愿能够在jq中完成所有这一切,而不是在bash中使用for循环(如果可能的话)。
预期输出
{ "时间":" 1460728600", " guest_nice":" 0", ... }
答案 0 :(得分:1)
You haven't specified exactly how you want the arrays to be combined, but one approach is to use transpose
, which in this case is effectively a kind of zip
. For example:
$ jq -n -c '[["a","b"], [1,2]] | transpose'
yields: [["a",1],["b",2]]
If you wanted an array of objects, then with the same input,
transpose | map( { (.[0]) : .[1] } )
would yield: [{"a":1},{"b":2}]
If your jq does not have transpose
, here is its definition:
# transpose a possibly jagged matrix, quickly;
# rows are padded with nulls so the result is always rectangular.
def transpose:
[range(0; (map(length) | max)) as $j
| [range(0; length) as $i | .[$i][$j] ] ] ;
Alternatively, if you would prefer a very brief zip
:
def zip: [range(0; .[0]|length) as $i | [.[0][$i], .[1][$i]]];
答案 1 :(得分:0)
这是一个解决方案,它处理一般情况,其中第一个数组包含键名,以下数组包含使用转置和 from_entries
的值 {h:.[0], v:.[1:][]} # {h:[keys], v:[values]}
| [.h, .v] # [ [keys], [values] ] ...
| [ transpose[] | {key:.[0], value:.[1]} ] # [ {"key":key, "value":value}, ... ]
| from_entries # { key:value, key:value, ... }
例如,如果此过滤器位于filter.jq
且data.json
包含
["time","guest_nice","guest","steal","softirq","irq","user","system","nice","iowait"]
[1460728600,0,0,0,0.45731,0,0.25108,11.74702,48.22465,0]
[1460728601,0,0,0,0.45732,0,0.25109,12.74703,49,0]
然后命令
jq -M -s -c -f filter.jq data.json
产生
{"time":1460728600,"guest_nice":0,"guest":0,"steal":0,"softirq":0.45731,"irq":0,"user":0.25108,"system":11.74702,"nice":48.22465,"iowait":0}
{"time":1460728601,"guest_nice":0,"guest":0,"steal":0,"softirq":0.45732,"irq":0,"user":0.25109,"system":12.74703,"nice":49,"iowait":0}