Freddy Roller
Freddy Roller

Reputation: 127

Using jq is there an easy way to combine json objects into an array

I have two simple jsons that I want to combine into one. I am trying to bake this into some automation so that I will not need to manually combine the two each time. Is there a way with jq to combine file1.json with file2.json so that the output looks like desired.json?

file1.json

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

file2.json

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

desired.json

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

Upvotes: 1

Views: 108

Answers (1)

pmf
pmf

Reputation: 36033

Explicitly

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

Implicitly

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

Or as suggested by @peak

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

Upvotes: 1

Related Questions