gpt4 book ai didi

json - 基于嵌套值使用 jq 过滤数组

转载 作者:行者123 更新时间:2023-12-03 23:18:28 28 4
gpt4 key购买 nike

我正在编写一些 bash 脚本来帮助自动化 AWS 资源的管理。我正在使用 aws-clijq ,到目前为止一切都很好。

我正在使用自定义标签标记我的资源。在某些情况下,我想根据 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"
}
]
}

关于json - 基于嵌套值使用 jq 过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43397216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com