How to check for presence of 'key' in jq before iterating over the values

前端 未结 3 1608
暖寄归人
暖寄归人 2021-01-31 14:23

I get Cannot iterate over null (null) from the below query because .property_history is not present in result object.

How do i che

相关标签:
3条回答
  • 2021-01-31 14:32

    You can use the select-expression in jq to do what you intend to achieve, something as,

    jq '.result | select(.property_history != null) | .property_history | map(select(.event_name == "Sold"))[0].date'
    "08/30/2004"
    
    0 讨论(0)
  • 2021-01-31 14:32

    You could use the ? post-fix operator:

    $ jq '.result 
      | .property_history? 
      | .[]
      | select(.event_name == "Sold") 
      | .date'
    "08/30/2004"
    
    0 讨论(0)
  • 2021-01-31 14:51
    jq '.result.property_history // empty | map(select(.event_name == "Sold"))[0:1][].date'`
    

    Trick is to use // together with empty.

    Another alternative is to use an additional select:

    jq '.result.property_history | select(.) | map(select(.event_name == "Sold"))[0:1][].date'`
    
    0 讨论(0)
提交回复
热议问题