Given:
a.json:
{"a": 1}
b.json:
{"b": 2}
Required contents of merged.json:
{"a": 1, "b": 2}
Question:
What's the jq
syntax to merge a.json and b.json into merged.json?
Tried:
jq '.* += input.*' a.json b.json
Thanks!
Given:
a.json:
{"a": 1}
b.json:
{"b": 2}
Required contents of merged.json:
{"a": 1, "b": 2}
Question:
What's the jq
syntax to merge a.json and b.json into merged.json?
Tried:
jq '.* += input.*' a.json b.json
Thanks!
As pointed out in the comments, you can use the -s option, which is usually fine, but for very large JSON objects, it might be more economical to use inputs
, e.g. with reduce
:
jq -n 'reduce inputs as $in (null; . + $in)' a.json b.json
Arbitrarily many files (or an arbitrarily long streams of JSON objects) can be processed in this way.
Of course, if the JSON objects do not have distinct keys, the above will potentially lose information, and an alternative to +
for merging objects might be appropriate.