0

file1.json

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

file2.json

[
  {
    "c": "c"
  },
  {
    "c": "c"
  }
]

desired output: file3.json

[
  {
    "a": "a",
    "b": "b",
    "c": "c"
  },
  {
    "a": "a",
    "b": "b",
    "c": "c"
  }
]
1
  • Just an FYI: In this case there will be identical number of objects in both files. Commented Apr 30, 2020 at 0:01

1 Answer 1

1

For this type of problem, transpose (think zip) can often be used to produce compact solutions. In the present case:

jq -s 'transpose | map(add)' file1.json file2.json

jq's transpose can also be used with arrays that are not of the same length.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.