我想使用jq(http://stedolan.github.io/jq/)来解析来自aws elb describe-load-balancers的json输出,并仅在AvailabilityZones包含特定值的位置返回名称和AZ。
这是代表源输出的部分编辑json:
{
"LoadBalancerDescriptions": [
{
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
],
"CanonicalHostedZoneName": "example.us-east-1.elb.amazonaws.com",
在指定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'
上述工作,但我想只选择它是否包含“us-east-1b”的值,而不管其他值。
答案 0 :(得分:5)
也许这可行:
aws elb describe-load-balancers --region us-east-1 | jq '.LoadBalancerDescriptions[] | select((.AvailabilityZones[] | select(. == "us-east-1b")) == "us-east-1b") | .CanonicalHostedZoneName, .AvailabilityZones'
我实际上用这样的输入进行了测试:
{
"LoadBalancerDescriptions": [
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
]
}
并运行此命令:
jq '.LoadBalancerDescriptions[] | select((.AvailabilityZones[] | select(. == "us-east-1b")) == "us-east-1b")' input_file
然后我得到了:
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
输入的另一个:
{
"LoadBalancerDescriptions": [
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
},
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c"
]
},
{
"AvailabilityZones": [
"us-east-1d"
]
}
]
}
输出:
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c",
"us-east-1d"
]
}
{
"AvailabilityZones": [
"us-east-1b",
"us-east-1c"
]
}
您可能可以使用该概念来验证表示数组的键是否包含类似的元素。