JQ如果那么其他 [英] JQ If then Else

查看:59
本文介绍了JQ如果那么其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对字段值进行if if else else?例如我正在使用的数据如下:

How would I do an if then else on the value of a field? Such as my data I am working with looks like:

  {"_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"
  }
}

这只是它的一小部分示例,但是我使用此表达式来解析它:

This is just a small sample of it, however I am using this expression to parse it:

{_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 If else项,如果有的话将其设为是,如果为null则设为否.我已经尝试了一些没有成功的方法,看起来很简单但在尝试解决问题时遇到了麻烦.

What I want to do is have an If then else for the Partner_attributions field that if there is something there make it a yes and if it is null make it No. I have tried a few things with no success, seems simple enough but having trouble trying to figure it out.

有人可以帮忙吗?

推荐答案

您需要创建一个实际对象以添加到第一个对象.将if表达式更改为:

You need to create an actual object to add to the first object. Change the if expression to:

{partner_attributions: (if (.partner_attributions | length) > 0 then "yes" else "no" end)}

这篇关于JQ如果那么其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆