Here is a solution which uses leaf_paths and select to find all the paths with a leaf "category" member and then extract the corresponding values with foreach
foreach (leaf_paths | select(.[-1] == "category")) as $p (
.
; .
; getpath($p)
)
If your input is in a file called input.json
and the above filter is in a file called filter.jq
then the shell command
jq -f filter.jq input.json
should produce
"cocker spaniel"
"dog"
You can use the -r
flag if you don't want the quotes in the output.
EDIT: I now realize a filter of the form foreach E as $X (.; .; R)
can almost always be rewritten as E as $X | R
so the above is really just
(leaf_paths | select(.[-1] == "category")) as $p
| getpath($p)