Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用JMESPath计算数组中的实例数_Arrays_Json_Syntax_Jmespath - Fatal编程技术网

Arrays 使用JMESPath计算数组中的实例数

Arrays 使用JMESPath计算数组中的实例数,arrays,json,syntax,jmespath,Arrays,Json,Syntax,Jmespath,在这个问题底部的JSON示例中,如何使用JMESPath计算数组“Tags”中的键/值对数 根据,我可以使用count()- Reservations[].Instances[].Tags[] | length(@) 例如,下面的表达式创建一个数组,该数组包含foo对象中的元素总数,后跟foo[“bar”]的值 然而,文件似乎不正确。使用JMESPath网站,查询Reservations[].Instances[].[count(@),Tags]yeilds结果[[null]]。然后我通过AW

在这个问题底部的JSON示例中,如何使用JMESPath计算数组
“Tags”
中的键/值对数

根据,我可以使用
count()-

Reservations[].Instances[].Tags[] | length(@)
例如,下面的表达式创建一个数组,该数组包含foo对象中的元素总数,后跟foo[“bar”]的值

然而,文件似乎不正确。使用JMESPath网站,查询
Reservations[].Instances[].[count(@),Tags]
yeilds结果
[[null]]
。然后我通过AWS命令行进行了测试,返回了一个错误-

未知函数:count()

实际上有没有一种使用JMESPath的方法

示例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"
                        }
                    ]
                }
            ]
        }
    ]
}

这里的答案是JMESPath文档令人震惊,出于某种原因,我看到了过时的文档(查看屏幕右下角以查看您正在查看的版本)

我可以使用
length()
函数执行我需要执行的操作-

Reservations[].Instances[].Tags[] | length(@)

我设法将length
length(Tags[*])
的用法合并到一个更大的语句中,我认为这很有用,希望与大家分享:

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

另一个选项:
length(Reservations[].Instances[].Tags[])
抱歉,我不小心记下了这个答案,现在无法编辑我的投票:((除非答案被编辑)。我会在有机会的时候修复它。不用担心。反正这只是伪造的互联网点……不过感谢你的解释。