Count the number of instances in an array using JMESPath
Asked Answered
O

2

21

In the example JSON at the bottom of this question, how can I count the number of key/value pairs in the array "Tags" using JMESPath?

According to the JMESPath documentation, I can do this using the count() function -

For example, the following expression creates an array containing the total number of elements in the foo object followed by the value of foo["bar"].

However, it seems that the documentation is incorrect. Using the JMESPath website, the query Reservations[].Instances[].[count(@), Tags] yeilds the result [ [ null ] ]. I then tested via the AWS command line and an error was returned -

Unknown function: count()

Is there actually a way of doing this using JMESPath?

Example JSON -

{
    "Reservations": [
        {
            "Instances": [
                {
                    "InstanceId": "i-asdf1234",
                    "InstanceName": "My Instance",
                    "Tags": [
                        {
                            "Value": "Value1",
                            "Key": "Key1"
                        },
                        {
                            "Value": "Value2",
                            "Key": "Key2"
                        },
                        {
                            "Value": "Value3",
                            "Key": "Key3"
                        },
                        {
                            "Value": "Value4",
                            "Key": "Key4"
                        }
                    ]
                }
            ]
        }
    ]
}
Onomasiology answered 26/8, 2016 at 8:51 Comment(0)
O
33

The answer here is that the JMESPath documentation is shocking, and for some reason I was seeing out of date documentation (check the bottom right corner of the screen to see what version you are viewing.

I can do what I need to do using the length() function -

Reservations[].Instances[].Tags[] | length(@)
Onomasiology answered 26/8, 2016 at 8:54 Comment(2)
Another option: length(Reservations[].Instances[].Tags[] )Microscopium
Note the space between ] and ).Calorimeter
O
19

I managed to incorporate this usage of length length(Tags[*]) within a larger statement I think is useful and wanted to share:

aws ec2 describe-instances --region us-west-2 --query 'Reservations[*].Instances[*].{id: InstanceId, ami_id: ImageId, type: InstanceType, tag_count: length(Tags[*])}' --profile prod --output table;

--------------------------------------------------------------------
|                         DescribeInstances                        |
+--------------+-----------------------+------------+--------------+
|    ami_id    |          id           | tag_count  |    type      |
+--------------+-----------------------+------------+--------------+
|  ami-abc123  |  i-redacted1          |  1         |  m3.medium   |
|  ami-abc456  |  i-redacted2          |  7         |  m3.xlarge   |
|  ami-abc789  |  i-redacted3          |  12        |  t2.2xlarge  |
+--------------+-----------------------+------------+--------------+
Osterman answered 12/6, 2018 at 23:36 Comment(2)
apologies, I accidentally marked this answer down, and am now unable to edit my vote :( (unless the answer is edited). I'll fix it up when I get the opportunity.Fitment
No worries. It's only fake internet points anyways... Thanks for the explanation though.Osterman

© 2022 - 2024 — McMap. All rights reserved.