我Cannot iterate over null (null)从下面的查询得到,因为.property_history在result对象中不存在.
.property_history在继续操作之前,如何检查密钥的存在map(...)?
我试过用类似的东西 sold_year= `echo "$content" | jq 'if has("property_history") then
map(select(.event_name == "Sold"))[0].date' else null end
原始查询:
sold_year=`echo "$content" | jq '.result.property_history | map(select(.event_name == "Sold"))[0].date'`
Run Code Online (Sandbox Code Playgroud)
JSON:
{
"result":{
"property_history":[
{
"date":"01/27/2016",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":0
},
{
"date":"12/15/2015",
"price_changed":0,
"price":899750,
"event_name":"Listed",
"sqft":2357
},
{
"date":"08/30/2004",
"price_changed":0,
"price":739000,
"event_name":"Sold",
"sqft":2357
}
]
}
}
Run Code Online (Sandbox Code Playgroud)