jq:如何将元素从一个数组添加到另一个数组

时间:2018-11-04 00:21:39

标签: arrays json jq

第一个数组文件1:

[{"1":"2"}, {"3":"4"}] 

file2中的第二个数组:

[{"a":"b"}, {"c":"d"}]

我尝试了“ jq -s -c'。[0] [] +。[1] []'file1 file2”。结果:

{"1":"2","a":"b"}
{"3":"4","a":"b"}
{"1":"2","c":"d"}
{"3":"4","c":"d"}

但是,我想要以下内容:

{"1":"2","a":"b"}
{"3":"4","c":"d"}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

(一种?)优雅的方式:

$ jq -n --argfile f1 file1.json --argfile f2 file2.json -f program.jq

其中program.jq是:

[$f1,$f2]
| transpose
| map(add)