I am struggling to merge two JSON arrays with jq, because I would like to remove duplicate keys in merged objects:
Edit: added a second key since the example was too simple.
file1.json :
[
{"a": 1, "value": 11},
{"b": 2},
{"c": 3}
]
file2.json :
[
{"a": 4, "value": 44},
{"b": 5},
{"d": 6}
]
Expected result:
[
{"a": 4, "value": 44},
{"b": 5},
{"c": 3},
{"d": 6}
]
jq add file1.json file2.json
duplicates the keys (I have two objects with key "a" in the array).
I tried many answers from the web, but everybody has his own use case and none worked directly. The closest is this one: JQ - Merge two arrays but I can't manage to make it work with files instead of string arguments.
My last attempt was
jq \
--slurpfile base file1.json \
--slurpfile params file2.json \
'$base + $params | unique_by(.Key)'