Skip to content
Advertisement

JQ – Deep child replace through wildcard search, but check parent presence with merge to the original JSON

This is a kind of extension to my previous question JQ – Deep child value replace through wildcard search and merge to the original JSON

I want to check the presence of a particular key in some parents before replacing the child. Loosely, translated to CSS selector, I want something like * > fixedKeyCheckPresence > * > fixedKeyCheckValue

Input:

JavaScript

Expected Output:

JavaScript

Tried: (.. | select(try has("fixedKeyCheckPresence")) | .fixedKeyCheckValue) |= (sub("^Foo$";"Bar"))

jq play: https://jqplay.org/s/_YlLSyTEc7

Advertisement

Answer

You can combine walk with .. :

JavaScript

.fixedKeyCheckPresence? takes care of find the object key fixedKeyCheckPresence and leaves everything else intact.

Then under .fixedKeyCheckPresence, we enumerate all sub-nodes with .. and select the object containing fixedKeyCheckValue.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement