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

2
  • Does this answer your question? How to merge 2 JSON objects from 2 files using jq? Commented Feb 24, 2020 at 8:55
  • I tried using the above link, but couldn't reach the solution Commented Feb 24, 2020 at 9:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.