0

I have a large json that looks similar to this

{
   "Report" : [
   {"blah" : "..."}
   ],
   "Actions" : [
      {
         "value" : "1",
         "properties" : {
            "name" : "abc",
            "age" : "2",
            "other": "test1"
          }
      },
      {
         "value" : "2",
         "properties" : {
            "name" : "def",
            "age" : "3",
            "other" : "test2"
          }
      }
   ]
}

and I need to change the value of "other" depending on the value of "age".

Now, I want the complete json to be output on the terminal, so I can move it to tmp file.

This command works but outputs only the Action block on the terminal

 jq '(.Actions[] | select (.properties.age == "3").properties.other = "no-test")'

This command prints the complete json, but rewrites the value for keys that should not be modified (notice "no-test" gets re-written for both ages 2 and 3).

jq '(. | select (.Actions[].properties.age == "3").Actions[].properties.other = "no-test")'

Please advise if there is a way to zone in on the block to the changed, yet output the complete json on the terminal.

Pradeep BS
  • 51
  • 1
  • 7

1 Answers1

1

Assignment prints the whole object with the assignment executed so you could assign a new value to .Actions of the modified Actions array

.Actions=([.Actions[] | if .properties.age == "3" then .properties.other = "no-test" else . end])

I used an if statement but we can use your code to do the same thing

.Actions=[.Actions[] | select (.properties.age == "3").properties.other = "no-test"]

I created these using jqplay.org which is a tool the dev put up, makes these really quick to debug

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Will Barnwell
  • 4,049
  • 21
  • 34