0

I would like to merge two json files with jq:

Inputs: test1.json

{
  "key1": "value1",
  "key2": "value2"
}

test2.json

{
  "key3": "value3",
  "key4": "value4"
}

Expected output:

{
  "key1": "value1",
  "key2": "value2",
  "extra": {
     "key3": "value3",
     "key4": "value4"
  }
}

I know that it would be possible two just merge them without extra key like that: jq -s '.[0] * .[1]' test1.json test2.json but that would not add those extra nesting level.

Murakami
  • 3,474
  • 7
  • 35
  • 89
  • 2
    Try `jq -s '.[0] + {extra: .[1]}' test1.json test2.json`, or simply `jq '.extra = input' test1.json test2.json`, or visit [How to merge 2 JSON objects from 2 files using jq?](https://stackoverflow.com/questions/19529688/how-to-merge-2-json-objects-from-2-files-using-jq) – pmf Nov 30 '22 at 11:43

0 Answers0