如何在字段的值上执行if if else?比如我正在使用的数据如下:
{"_key": "USCA3DC_8f4521822c099c3e",
"partner_attributions": ["This business is a Yelp advertiser."],
"showcase_photos": [
["Mathnasium of Westwood - Westwood, CA, United States. Nice and caring instructors", "http://s3-media1.fl.yelpcdn.com/bphoto/KeKAhvy2HHY4KGpvA24VaA/ls.jpg"],
["Mathnasium of Westwood - Westwood, CA, United States. Prize box and estimation jar!", "http://s3-media3.fl.yelpcdn.com/bphoto/lJWHHCAVaUMfeFD7GDKtHw/ls.jpg"],
["Mathnasium of Westwood - Westwood, CA, United States. New table setup!!!!", "http://s3-media2.fl.yelpcdn.com/bphoto/kVYJrYqDRHPOH4F2uTuFVg/ls.jpg"],
["Mathnasium of Westwood - Westwood, CA, United States. Halloween party", "http://s3-media3.fl.yelpcdn.com/bphoto/wKm5KjF0V8MsPTVSuofPEQ/180s.jpg"],
["Mathnasium of Westwood - Westwood, CA, United States", "http://s3-media4.fl.yelpcdn.com/bphoto/r2981msJm0c1ocU09blb1A/180s.jpg"],
["Mathnasium of Westwood - Westwood, CA, United States", "http://s3-media3.fl.yelpcdn.com/bphoto/r2Vgo18YKeUojDvjQMRF_A/180s.jpg"]
],
"review_count": "24",
"yelp_id": "t7WyXcABE3xj20G-UqXalA",
"rating_value": "5.0",
"coordinates": {
"latitude": "34.042568",
"longitude": "-118.431038"
}
}
这只是它的一小部分,但是我使用这个表达式来解析它:
{_key, last_visited, name, phone, price_range, rating_value, review_count, updated, url, website, yelp_id} + (if (.partner_attributions | length) > 0 then .partner_attributions == "yes" else .partner_attributions == "no" end) + ([leaf_paths as $path | {"key": $path | map(tostring) | join("_"),"value": getpath($path)}] | from_entries)
我想要做的是在Partner_attributions字段中有一个if then else,如果有什么东西使它成为是,如果它为null,则使它为No。我尝试了一些没有成功的事情,看起来很简单但无法弄清楚。
有人可以帮忙吗?
答案 0 :(得分:0)
您需要创建一个实际对象以添加到第一个对象。将if
表达式更改为:
{partner_attributions: (if (.partner_attributions | length) > 0 then "yes" else "no" end)}
答案 1 :(得分:0)
或许更好:
.partner_attributions |= (if length > 0 then "yes" else "no" end)
这只是更新一个字段而不修改任何其他字段。