user7644509
user7644509

Reputation: 130

Linux Update JSON with Array

How would I update a JSON with an array nested inside? I got stuck trying out with jq using. It cuts off items in "b" so only 1 is inside it.

jq '.items[1].b."1" = "changed"' <<< cat file.json

So example if a json the following is like:

{
    "href": "1234",
    "list": [{
        "a": {
            "dummy": "thing"
        },
        "b": {
            "0": "thing",
            "1": "thing", <--- ex. I want to change this
            "2": "thing"
        }
    }]
}

Desired Result

# Result that I want
{
    "href": "1234",
    "list": [{
        "a": {
            "dummy": "thing"
        },
        "b": {
            "0": "thing",
            "1": "changed", <--- this changed
            "2": "thing"
        }
    }]
}

Upvotes: 1

Views: 101

Answers (1)

tshiono
tshiono

Reputation: 22022

Would you try the following:

jq '(.list[].b."1")="changed"' file.json

Output:

{
  "href": "1234",
  "list": [
    {
      "a": {
        "dummy": "thing"
      },
      "b": {
        "0": "thing",
        "1": "changed",
        "2": "thing"
      }
    }
  ]
}

Upvotes: 2

Related Questions