jq根据列表中的值过滤JSON数组
如果以下条件匹配,例如.[].Conditions [].Values []
,我想使用 jq
返回 RuleArn
值.>具有与 app.fantastic.com
I want to use jq
to return the RuleArn
value if the following condition matches i.e. .[].Conditions[].Values[]
has an element that matches app.fantastic.com
我的JSON数组:
[
{
"Conditions": [
{
"Field": "http-header",
"HttpHeaderConfig": {
"Values": [
"dark"
],
"HttpHeaderName": "Environment"
}
},
{
"Values": [
"app.fantastic.com"
],
"Field": "host-header",
"HostHeaderConfig": {
"Values": [
"app.fantastic.com"
]
}
}
],
"IsDefault": false,
"Priority": "3",
"RuleArn": "iwantthisvalue"
}
]
我已经尝试过这些:
|jq -r'.[] |select(.Conditions [].Values [] =="app.fantastic.com")'
和
|jq -r'.[] |select(.Conditions [].Values [] | has("app.fantastic.com"))'
我收到以下错误:
jq:错误(处:144):无法使用字符串条件"索引数组
并与此:
|jq'.[].Conditions [].Values |索引("app.fantastic.com")== 0 |.RuleArn'
这是我得到的错误:
jq:错误(在47处):无法使用字符串"RuleArn"对布尔值进行索引
注意:我不希望使用AWS cli的-query
解决方案,因为我已经使用了-query
来缩小尺寸.我想使用 jq
进一步过滤的JSON有效负载.
Note: I do not want a solution using --query
of AWS cli as I use --query
already to get a smaller JSON payload which I want to filter further using jq
.
您遇到的困难的根本原因是问题陈述不正确:.[].Conditions [].Values []
与您的JSON不对应.
The root of the difficulty you are running into is that the problem statement is improper: .[].Conditions[].Values[]
does not correspond to your JSON.
使用jq 1.5或更高版本,您可以使用以下过滤器:
Using jq 1.5 or later, you could use the following filter:
.[]
| first(select(.Conditions | .. | objects
| select(has("Values") and
(.Values|index("app.fantastic.com")))))
| .RuleArn
由于.values出现在多个地方,因此尚不清楚确切的要求是什么,但是您可能还需要考虑:
Since .Values occurs in several places, it's not clear what the precise requirements are, but you might also wish to consider:
.[]
| select(.Conditions[].Values? | index("app.fantastic.com"))
| .RuleArn
或(效率较低,但可与jq 1.4或更高版本一起使用):
or (less efficiently, but it works with jq 1.4 or later):
.[]
| select(.Conditions[].Values[]? == "app.fantastic.com")
| .RuleArn