oscar77
oscar77

Reputation: 11

Want to add more parent keys to JSON with JQ

I have this JSON:

[
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4,
    "key5": {
      "subkey1": "subvalue1",
      "subkey2": "subvalue2",
      "subkey3": "subvalue3"
    }
  }
]

i want to build a new JSON using JQ, and add more items, let me explain, i want to get this:

{
  "NEWKEY1": "NEWVALUE2",
  "NEWKEY2": [
    {
      "NEWKEY3": "UPSNEWVALUE3",
      "NEWKEY4": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
        "key4": "value4,
        "key5": {
          "subkey1": "subvalue1",
          "subkey2": "subvalue2",
          "subkey3": "subvalue3"
        }
      }
    }
  ]
}

How i can get that?

Thanks all

Upvotes: 1

Views: 36

Answers (1)

peak
peak

Reputation: 116880

If data.json contains the new data, and template.json contains the template with NEWKEY1, etc, then the following invocation produces the desired output:

jq --argfile in data.json '.NEWKEY2[0].NEWKEY4 = $in[0]' template.json

Quibble

Yes, I know that the jq manual deprecates --argfile, so feel free to use one of the many alternatives, but all currently available versions of jq support it, which is more than can be said for the similar alternatives....

Upvotes: 1

Related Questions