Recursive search values by key

后端 未结 2 556
长发绾君心
长发绾君心 2020-12-10 05:17

I have a JSON like this:

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


        
相关标签:
2条回答
  • 2020-12-10 06:03

    Here is a solution that uses tostream and select

      tostream
    | select(length==2 and .[0][-1]=="error" and .[1]!=null) as [$p,$v]
    | $v
    
    0 讨论(0)
  • 2020-12-10 06:17

    Use .. to iterate recursively, and get all the .error values. If they're null, remove them:

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

    Alternatively, instead of using empty you can select the elements that are strings with strings:

    jq '.. | .error? | strings'
    
    0 讨论(0)
提交回复
热议问题