Richard
Richard

Reputation: 23

Merge json files - Concatenate into a single dict json file

here are 3 JSON files

File1

{
  "component1": [
  ]
}

File2

{
  "component2": [
  ]
}

File3

{
  "component3": [
  ]
}

Don't find the jq command line that would give this JSON file as jq output:

{
  "components": {
     "component1": [
      ],
     "component2": [
      ],
     "component3": [
      ]
   }
}

Many thanks for your support Best Regards.

Upvotes: 1

Views: 173

Answers (2)

peak
peak

Reputation: 116680

You can simply use add, e.g.

jq -s '{components: add}' file{1..3}.json

or:

jq -n '{components: [inputs]|add}' file{1..3}.json

Upvotes: 1

Inian
Inian

Reputation: 85550

Iterate over the input objects one a time from inputs and append it to the components using the reduce function

jq -n 'reduce inputs as $d (.; .components += $d )' file{1..3}.json

Upvotes: 1

Related Questions