【问题标题】:Using jq is there an easy way to combine json objects into an array使用 jq 是否有一种简单的方法可以将 json 对象组合成一个数组
【发布时间】:2021-11-11 21:57:01
【问题描述】:

我有两个简单的 json,我想将它们合并为一个。我正在尝试将其烘焙到一些自动化中,这样我就不需要每次都手动组合两者。 jq 有没有办法将 file1.json 与 file2.json 结合起来,使输出看起来像 desired.json?

file1.json

[
  [
    "1",
    "2"
  ],
  [
    "a",
    "b"
  ]
]

file2.json

[
  [
    "3"
  ],
  [
    "c"
  ]
]

desired.json

[
  [
    "1",
    "2",
    "3"
  ],
  [
    "a",
    "b",
    "c"
  ]
]

【问题讨论】:

标签: json jq


【解决方案1】:

明确

jq -s '[map(.[0][]),map(.[1][])]'  file1.json file2.json

隐式

jq -s 'transpose | map(flatten)'  file*.json

或者按照@peak的建议

jq -s 'transpose | map(add)' file*.json

【讨论】:

  • 谢谢你,这正是我想要的!
  • flatten 是一个雷区,甚至可能是不必要的低效。更好用 add: jq -s 'transpose | map(add)'
猜你喜欢
  • 2020-09-18
  • 2019-03-24
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
相关资源
最近更新 更多