简体   繁体   English

使用jq解析AWS CLI的json输出

[英]Using jq to parse json output of AWS CLI

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包含特定值的地方返回名称和AZ。

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”的值,而不管其他值。

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. 您可能可以使用该概念来验证表示数组的键是否包含类似的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM