Merge JSON objects using jq

Merging objects in jq is handled natively since 1.14.

Merge objects using jq

Given a file named example.json

1
2
3
4
5
{
    "key": "value1",
    "anotherKey": "value2",
    "oneMoreKey": "value3"
}

and another file named example2.json,

1
2
3
4
5
{
  "key1": "value2",
  "key2": "value3",
  "key4": "value4"
}

merging the two objects can be completed using:

1
jq -s '.[0] * .[1]' example.json example2.json 

Output:

1
2
3
4
5
6
7
8
{
  "key1": "value2",
  "key2": "value3",
  "key4": "value4",
  "key": "value1",
  "anotherKey": "value2",
  "oneMoreKey": "value3"
}