Search code examples
aws-cliamazon-route53jmespath

JMESPath query with boolean values?


I'm trying to develop the expression to query Route53 and pull out the ID of our private zone.

Problem is the query string returns an empty array.

Base data below — running aws route53 list-hosted-zones:

{
    "HostedZones": [
        {
            "Id": "/hostedzone/Z030998332ZE45DV5X8L3",
            "Name": "dev.initech.com.",
            "CallerReference": "b00898ec-bd53-4551-8002-72a0822325e8",
            "Config": {
                "Comment": "Mainly used for certificates.",
                "PrivateZone": false
            },
            "ResourceRecordSetCount": 10
        },
        {
            "Id": "/hostedzone/Z062450333HHCLR5EM9XC",
            "Name": "dev.initech.com.",
            "CallerReference": "1a8f22d6-sam3-490b-8b76-9bc27bbdeb02",
            "Config": {
                "Comment": "",
                "PrivateZone": true
            },
            "ResourceRecordSetCount": 9
        }
    ]
}

I went off the AWS guide and came up with

aws route53 list-hosted-zones \
  --query 'HostedZones[*].Config[?PrivateZone==`true`].Id'

But I got an empty array.
I figured Config.PrivateZone is a boolean value so I tried

aws route53 list-hosted-zones --query 'HostedZones[*].Config[?PrivateZone==true].Id'

No error but still got an empty array.

I would expect to see something like

[
    "/hostedzone/Z062450333HHCLR5EM9XC"
]

What am I doing wrong?


I figured out how to do it with jq via

jq '.[] | select(.Config.PrivateZone == true).Id' foo.json

But would still like to know how to do it in JMESPath just because it's bugging me now.


Solution

  • You were right doing [?PrivateZone == true] instead of [?PrivateZone == `true`], what you missed is the fact that a filter needs to happen on an array, and HostedZones[].Config is an object (or hash).

    Because what you are actually looking for is a list of HostedZones that match your requirement, the filter needs to happen at that level, and so the filter needs to check that Config.PrivateZone == true.

    Giving you the query:

    HostedZones[?Config.PrivateZone].Id
    

    And as you can see, you don't even need the == true part, as the filter is already going to evaluate a boolean, it is useless.