![](/img/trans.png)
[英]How to filter an array of objects based on values of string in an inner array with jq
[英]Filter array with jq based on nested values
我正在编写一些bash脚本来帮助自动管理AWS资源。 我正在使用aws-cli
和jq
,到目前为止,一切都很好。
我正在用自定义标签标记资源。 在某些情况下,我想基于自定义标记的Key
和Value
来过滤资源列表。 但是我很难制定一个简洁的jq
查询来做到这一点。
因此,例如,如果我的ec2实例的(修整后的)JSON输出类似于:
[
{
"PublicIpAddress": "11.22.33.44",
"PrivateIpAddress": "55.66.77.88",
"Tags": [
{
"Value": "live199.blah.com",
"Key": "Name"
},
{
"Value": "live-standalone",
"Key": "hc-class"
}
]
}
]
[
{
"PublicIpAddress": "111.222.333.444",
"PrivateIpAddress": "555.666.777.888",
"Tags": [
{
"Value": "staging99.blah.com",
"Key": "Name"
},
{
"Value": "staging-standalone",
"Key": "hc-class"
}
]
}
]
...并且我需要找到其中Tags.Key == "hc-class"
和Tags.Value = "staging-standalone"
的条目,我该如何使用jq
简洁地实现它?
任何帮助,不胜感激。
使用给定的输入,以下过滤器将产生输出,如下所示:
.[] | select(any(.Tags[]; .Key == "hc-class" and .Value == "staging-standalone"))
输出:
{
"PublicIpAddress": "111.222.333.444",
"PrivateIpAddress": "555.666.777.888",
"Tags": [
{
"Value": "staging99.blah.com",
"Key": "Name"
},
{
"Value": "staging-standalone",
"Key": "hc-class"
}
]
}
我对JQ很陌生,但是我觉得这里有一个解决方案:
https://jqplay.org/s/DljtxNX_72
select((.[0].Tags[1].Key) == "hc-class" and (.[0].Tags[1].Value) == "staging-standalone")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.