【问题标题】:jq: how to add elements from an array into another array one by onejq:如何将一个数组中的元素一个一个地添加到另一个数组中
【发布时间】:2018-11-04 00:21:38
【问题描述】:

第一个数组文件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"}

感谢您的帮助。

【问题讨论】:

    标签: arrays json jq


    【解决方案1】:

    一种(?)优雅的方式:

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

    program.jq 在哪里:

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

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 2014-07-07
      相关资源
      最近更新 更多