Sandeep Sharma
Sandeep Sharma

Reputation: 141

Json file editing through jq

I have json

{
    "file1": [{
        "username": "myname",
        "groupname": "mypassword",
        "environment": [{
            "name": "UMASK",
            "value": "022"
        },
        {
            "name": "DEBUG",
            "value": "2"
        }]
    }]
}

and want to change the value of DEBUG to 5.

Tried with below command

jq .file1[0].environment sandeep.json |jq '(.[] |select(.name == "DEBUG") | .value) |= "5"'

this will return me specific portion of json like

[
  {
    "name": "UMASK",
    "value": "022"
  },
  {
    "name": "DEBUG",
    "value": "5"
  }
]

but I want to see full json with changed value

{
    "file1": [{
        "username": "myname",
        "groupname": "mypassword",
        "environment": [{
            "name": "UMASK",
            "value": "022"
        },
        {
            "name": "DEBUG",
            "value": "5"
        }]
    }]
}

Please suggest me

Upvotes: 1

Views: 179

Answers (1)

hek2mgl
hek2mgl

Reputation: 157947

It should be:

jq '(.file1[].environment[]|select(.name=="DEBUG").value) |= 5' file.json

Output:

{
  "file1": [
    {
      "username": "myname",
      "groupname": "mypassword",
      "environment": [
        {
          "name": "UMASK",
          "value": "022"
        },
        {
          "name": "DEBUG",
          "value": 5
        }
      ]
    }
  ]
}

Upvotes: 1

Related Questions