Jq: search property by name deep inside unknown input structure and print path

The title says it all: the jq program takes an input JSON document whose structure I am only vaguely familiar with, and I want it to print the path to all the properties inside it that have a specific name.

+3


source to share


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







All Articles