Darth.Vader
Darth.Vader

Reputation: 6271

using jq to update a json value

I have a JSON file like this:

{
  "cmp": {
    "vr1": {
      "vr2": {
        "mpa": [
          "foo1",
          "foo2"          
        ]
      }
    }
  },  
  "at": "",
  "pl": {
    "accountId": ""
  },
  "sv": {
    "accountId": ""
  }
}

I am trying to use jq to replace the values in the JSON array mpa from ["foo1", "foo2"] to ["bar1", "bar2"]. How can I do this?

My attempt:

jq -r -e \
    .[].cmp.vr1.vr2.mpa=["bar1", "bar2"]
    < /tmp/json-file.json

What am I missing?

Upvotes: 0

Views: 209

Answers (1)

0stone0
0stone0

Reputation: 43894

.[] are Array/Object Value Iterator's.

Since you're dealing with an object, no need to use an iterator, you can target your array like so:

jq '.cmp.vr1.vr2.mpa = [ "bar1", "bar2" ]' input.json

Snippet

Upvotes: 1

Related Questions