Jq: search property by name deep inside unknown input structure and print path
1 answer
Suppose you want to find paths to objects with a key named "b". One approach would be to use paths (objects) as shown here:
def data: {a:{b:1,c:{b:2}}};
data
| paths(objects | has("b") )
Or a little more efficient:
data
| paths
| select( .[-1] == "b" )
| .[:-1]
Call: jq -n -c -f program.jq
Output:
["a"]
["a","c"]
+2
source to share