1

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"
        }
    }]
}
2
  • 1
    Your code doesn't match the sample input Commented Mar 10, 2020 at 6:55
  • Updated the question so it is more clear. Commented Mar 10, 2020 at 17:41

1 Answer 1

2

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"
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.