My question is similar to Unix jq parsing wildcards but want to merge to the original JSON.
Say, input JSON:
{
"a": {
"1": {
"c": "text1"
},
"999": {
"c": "text99"
}
}
}
I want to manipulate the inner "c": "text1"
and modify it to "c": "newtext"
. But, also need to merge with the original JSON. IOW, it is not about extracting, but about manipulating.
Expected output:
{
"a": {
"1": {
"c": "newtext"
},
"999": {
"c": "text99"
}
}
}
I tried:
.. | .c? |= (sub("^text1$";"newtext"))
But, it throws null (null) cannot be matched, as it is not a string
jqplay: https://jqplay.org/s/2nFAus6Umz
CodePudding user response:
Just walk
the path with the expression to select an object type when .c
equals to your desired value
jq 'walk(if type == "object" and .c == "text1" then .c |= "newtext" else . end)'
CodePudding user response:
Along the lines of your attempt:
(.. | objects | select(has("c")) | .c) |= (sub("^text1$";"newtext"))
or in order of increasing brevity:
(.. | select(try has("c")) | .c) |= (sub("^text1$";"newtext"))
(.. | select(has("c")?) | .c) |= (sub("^text1$";"newtext"))