Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append JSON Objects using jq

Tags:

json

edit

jq

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 ?

like image 454
theapache64 Avatar asked Dec 14 '22 15:12

theapache64


1 Answers

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"
  }
}
like image 154
RomanPerekhrest Avatar answered Dec 18 '22 10:12

RomanPerekhrest