bash 使用 jq 解析 AWS CLI 的 json 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18796515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jq to parse json output of AWS CLI
提问by cp_clegg
I would like to use jq (http://stedolan.github.io/jq/) to parse the json output from aws elb describe-load-balancers and return the name and AZs only where AvailabilityZones contains a specific value.
我想使用 jq ( http://stedolan.github.io/jq/) 解析来自 aws elb describe-load-balancers 的 json 输出,并仅在 AvailabilityZones 包含特定值的情况下返回名称和可用区。
Here is partial redacted json representing the source output:
这是代表源输出的部分编辑 json:
{
"LoadBalancerDescriptions": [
{
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
],
"CanonicalHostedZoneName": "example.us-east-1.elb.amazonaws.com",
I have only been able to get this to work when specifiying the full list of values for the AvailabilityZones key.
我只有在为 AvailabilityZones 键指定完整的值列表时才能使其工作。
$ aws elb describe-load-balancers --region us-east-1 |jq '.LoadBalancerDescriptions[] | select(.AvailabilityZones == ["us-east-1b", "us-east-1c", "us-east-1d"]) | .CanonicalHostedZoneName, .AvailabilityZones'
The above works, but I want to just select if it contains a value for "us-east-1b", regardless of the other values.
以上工作,但我只想选择它是否包含“us-east-1b”的值,而不管其他值。
采纳答案by konsolebox
Perhaps this could work:
也许这可以工作:
aws elb describe-load-balancers --region us-east-1 | jq '.LoadBalancerDescriptions[] | select((.AvailabilityZones[] | select(. == "us-east-1b")) == "us-east-1b") | .CanonicalHostedZoneName, .AvailabilityZones'
I actually tested with an input like this:
我实际上用这样的输入进行了测试:
{
"LoadBalancerDescriptions": [
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
]
}
And ran this command:
并运行此命令:
jq '.LoadBalancerDescriptions[] | select((.AvailabilityZones[] | select(. == "us-east-1b")) == "us-east-1b")' input_file
Then I got:
然后我得到:
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
Another for input:
另一个用于输入:
{
"LoadBalancerDescriptions": [
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
},
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c"
]
},
{
"AvailabilityZones": [
"us-east-1d"
]
}
]
}
Output:
输出:
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c"
]
}
You probably could use the concept to validate if a key representing an array contains an element like it.
您可能可以使用这个概念来验证表示数组的键是否包含类似的元素。