Recursive Key Search Values

I have such JSON:

{ 
  "A": { "error": null },
  "B": { "C": {"error": "error string"}},
  "C": { "D": {"error": null}},
  "D": { "error": "err str"}
}

end I want to find all error values that are not null .

In my example, it should return

"error string"
"err str"

How can i do this? Is this possible with jq ?

+4
source share
2 answers

Use ..to repeat the iteration and get all the values .error. If they are null, delete them:

jq '.. | .error? // empty'

Alternatively, instead emptyyou can select elements that are strings, with strings:

jq '.. | .error? | strings'
+7
source

, tostream

  tostream
| select(length==2 and .[0][-1]=="error" and .[1]!=null) as [$p,$v]
| $v
0

Source: https://habr.com/ru/post/1648377/


All Articles