Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jq to parse json output of AWS CLI

Tags:

json

bash

jq

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.

like image 911
cp_clegg Avatar asked Sep 13 '13 23:09

cp_clegg


People also ask

What are the output format for AWS CLI?

The AWS CLI supports the following output formats: json – The output is formatted as a JSON string. yaml – The output is formatted as a YAML string. yaml-stream – The output is streamed and formatted as a YAML string.

What is AWS jq?

JQ enables us to filter, map and count and so on against JSON data. For example. $ cat sample.json { "id": "123456", "name": "Kai Sasaki", "address": "Tokyo", "language": [ "Japanese", "English", "Java" ] } You can filter “id” with.

What is jq command used for?

jq command is used not only for reading JSON data but also to display data by removing the particular key. The following command will print all key values of Students. json file by excluding batch key. map and del function are used in jq command to do the task.


1 Answers

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.

like image 194
konsolebox Avatar answered Sep 21 '22 21:09

konsolebox