How to rewrite the exact value in jq, referencing the top element

I have a big json that looks like 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 full json to be output to the terminal, so I can move it to the tmp file.

This command works, but only displays an Action block on the terminal

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

This command prints full json, but overwrites the value for keys that should not be changed (the "no-test" notification is overwritten 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 make changes to the zone on the block, but output the full json to the terminal.

0
source share
1 answer

, .Actions Actions

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

if, ,

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

jqplay.org, , ,

0

All Articles