2

I have multiple JSON files one.json, two.json, three.json with the below format and I want to create a consolidated array from them using jq. So, from all the files I want to extract Name and Value field inside the Parameters and use them to create an array where the id value will be constructed from the Name value and value field will be constructed using Value field value.

input:
one.json:

{
  "Parameters": [
    {
      "Name": "id1",
      "Value": "one",
      "Version": 2,
      "LastModifiedDate": 1581663187.36

    }
  ]
}

two.json

{
  "Parameters": [
    {
      "Name": "id2",
      "Value": "xyz",
      "Version": 2,
      "LastModifiedDate": 1581663187.36

    }
  ]
}

three.json

{
  "Parameters": [
    {
      "Name": "id3",
      "Value": "xyz",
      "Version": 2,
      "LastModifiedDate": 1581663187.36

    }
  ]
}

output:

[
  {
    "id": "id1",
    "value": "one"
  },
  {
    "id": "id2",
    "value": "xyz"
  },
  {
    "id": "id3",
    "value": "xyz"
  }
]

How to achieve this using jq

Inian
  • 80,270
  • 14
  • 142
  • 161
dks551
  • 1,113
  • 1
  • 15
  • 39
  • Does this answer your question? [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) – Adam Strauss Feb 24 '20 at 08:55
  • I tried using the above link, but couldn't reach the solution – dks551 Feb 24 '20 at 09:02

1 Answers1

4

You can use a reduce expression instead of slurping the whole file into memory (-s); by iterative manipulation of the input file contents and then appending the required fields one at a time.

jq -n 'reduce inputs.Parameters[] as $d (.; . + [ { id: $d.Name, value: $d.Value } ])' one.json two.json three.json

The -n flag is to ensure that we construct the output JSON data from scratch over the input file contents made available over the inputs function. Since reduce works in an iterative manner, for each of the object in the input, we create a final array, creating the KV pair as desired.

Inian
  • 80,270
  • 14
  • 142
  • 161