How to change a nested object using jq

Considering this

{ "some": "property", "nested": { "hello": "world" } } 

I want to get this result using jq

 { "some": "property", "nested": { "hello": "world", "freshly": "added" } } 

So how can I add the field just added? I don’t know how many properties are at the root level (and I want to save them all), I only know the name of the nested object (here “nested”), the name of the property that I would like to add (here “fresh”) and its value.

+5
source share
2 answers

Just assign a new value to the nested object.

 .nested.freshly = "added" 
+9
source

Ok, I found out how to do it. If you have a better solution, you can more welcome it here.

 jq '.nested=(.nested + {"freshly": "added"})' 
+1
source

All Articles