Append JSON Objects using jq

theapache64

I've below JSON structure

{

    "a": "aVal",
    "x": {
      "x1": "x1Val",
      "x2": "x2Val"
    }
    "y": {
      "y1": "y1Val"
    }
}

I want to add "x3": "x3Val","x4": "x4Val" to x. So the output should be

{
    ...
    "x": {
      ....
      "x3": "x3Val",
      "x4": "x4Val",
    }
    ...
}

Is it possible using jq ?

RomanPerekhrest

Of course, it's pretty simple for jq:

jq '.x += {"x3": "x3Val","x4": "x4Val"}' file.json

The output:

{
  "a": "aVal",
  "x": {
    "x1": "x1Val",
    "x2": "x2Val",
    "x3": "x3Val",
    "x4": "x4Val"
  },
  "y": {
    "y1": "y1Val"
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

append json array using jq

Append string to json using jq

Append multiple dummy objects to json array with jq

Append an array to a json using jq in BASH

Add JSON objects to array using jq

How to parse json objects not array using jq

Parse JSON and iterate objects using jq

Using jq to extract multiple json objects

how to merge two JSON objects using jq?

Find common keys in JSON objects using jq

Making an array of objects from json using jq

Merge two complex JSON objects (using jq)

Replace objects in json with their sum using JQ

How to append an array of json into a json file using jq?

Find / Replace / Append JSON String in bashscript without using jq

Parse json using jq utility & append fields together

append a json block before specific line using jq

Use a value from one JSON to append in another using jq

How to create array of json objects using jq and set to var in bash

change a particular element inside array of json objects using jq

Flatten array for properties in nested array of JSON objects using jq

How to sort a stream of json objects by field value using jq

How concatenate multiple json objects with a delimiter comma using jq

How to merge 2 JSON objects from 2 files using jq?

jq: Conditional insert using "lookup" & "target" JSON objects

How can I filter an array of json objects using jq?

Using multiple wildcards in jq to select objects in a JSON file

Mapping over a JSON array of objects and processing values using JQ

Convert json single-item arrays to objects using bash/jq