7

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.

Here is partial redacted json representing the source output:

{
  "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.

$ 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.

2
  • I guess a more general way to ask this is how do I us jq to test a key containing multiple values for equality of just one of those values? Commented Sep 13, 2013 at 23:27
  • i want to parse the output of aws ec2 describe instances. can you help Commented Mar 28, 2014 at 5:09

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, I don't think that works. Try that on an input_file that has three groups of AvailabilityZones, one like what you used, plus one with just us-east-1b and us-east-1c, and then a third with only us-east-1d. I would expect the query to return the first two AvailabiltyZones but not the third however the query only returns the first.
@user2778073 I did make the solution with that intention. Testing it again returns two results as you expect.
You are completely right. Thanks you for taking the time to clarify but it was a mistake on my part with the json structure in my test input_file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.