最佳答案
我从下面的查询中得到 Cannot iterate over null (null)
,因为 .property_history
不存在于 result
对象中。
在使用 map(...)
之前,我如何检查 .property_history
密钥的存在?
我尝试使用诸如 < code > sell _ year = ‘ echo“ $content”| jq‘ if has (“ property _ history”)之类的命令 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'`
杰森:
{
"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
}
]
}
}