Use jq to append array contents from multiple files into one master array

Advertisements

I’m attempting to use jq to combine the data of many JSON files. However, most solutions I tried clobber the data and I end up losing most of it.

Each file looks something like this

{
  "Brand":"brand",
  "Fulfilled":5600,
  "Total":5626,
  "Data":[
    {
      "data":"data1"
    },
    {
      "data":"data2"
    }
  ]
}

I want to pull the contents from the main ‘Data’ array from each file and make it look something like this:

{
  "Everything":[
    {
      "data":"data1"
    },
    {
      "data":"data2"
    },
    {
      "data":"data3"
    },
    {
      "data":"data4"
    }
  ]
}

>Solution :

You can use inputs in combination with the -n flag to address all input documents:

jq -n '{Everything: [inputs.Data[]]}' *.json
{
  "Everything": [
    {
      "data": "data1"
    },
    {
      "data": "data2"
    },
    {
      "data": "data3"
    },
    {
      "data": "data4"
    }
  ]
}

Demo

Leave a ReplyCancel reply