Count the numer of instance in an array using JMESPath









up vote
7
down vote

favorite
1












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"

]

]

]










share|improve this question



























    up vote
    7
    down vote

    favorite
    1












    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"

    ]

    ]

    ]










    share|improve this question

























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      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"

      ]

      ]

      ]










      share|improve this question















      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"

      ]

      ]

      ]







      arrays json syntax jmespath






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 21:55









      dreftymac

      15.6k2187153




      15.6k2187153










      asked Aug 26 '16 at 8:51









      David Gard

      4,4571863129




      4,4571863129






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          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(@)





          share|improve this answer




















          • Another option: length(Reservations.Instances.Tags )
            – haschdl
            Feb 16 at 17:01

















          up vote
          1
          down vote













          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 |
          +--------------+-----------------------+------------+--------------+





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39162209%2fcount-the-numer-of-instance-in-an-array-using-jmespath%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            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(@)





            share|improve this answer




















            • Another option: length(Reservations.Instances.Tags )
              – haschdl
              Feb 16 at 17:01














            up vote
            7
            down vote



            accepted










            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(@)





            share|improve this answer




















            • Another option: length(Reservations.Instances.Tags )
              – haschdl
              Feb 16 at 17:01












            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            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(@)





            share|improve this answer












            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(@)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 26 '16 at 8:54









            David Gard

            4,4571863129




            4,4571863129











            • Another option: length(Reservations.Instances.Tags )
              – haschdl
              Feb 16 at 17:01
















            • Another option: length(Reservations.Instances.Tags )
              – haschdl
              Feb 16 at 17:01















            Another option: length(Reservations.Instances.Tags )
            – haschdl
            Feb 16 at 17:01




            Another option: length(Reservations.Instances.Tags )
            – haschdl
            Feb 16 at 17:01












            up vote
            1
            down vote













            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 |
            +--------------+-----------------------+------------+--------------+





            share|improve this answer
























              up vote
              1
              down vote













              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 |
              +--------------+-----------------------+------------+--------------+





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                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 |
                +--------------+-----------------------+------------+--------------+





                share|improve this answer












                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 |
                +--------------+-----------------------+------------+--------------+






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 12 at 23:36









                runamok

                5951519




                5951519



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39162209%2fcount-the-numer-of-instance-in-an-array-using-jmespath%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo