根据多个键/值对的存在过滤输出

时间:2017-01-06 10:13:36

标签: jmespath

使用JMESPath,是否可以根据输入中是否存在多个键/值对来过滤输出?

从下面的示例JSON中,我想要做的只是提取Tags中包含这些键/值对的对象 -

  

环境/ ABC
  项目/项目2

我能得到的最接近的是选择一个匹配的Tag,但我也需要该对象的其余部分,我还需要匹配另一个键/值对 -

Stacks[*].Tags[?Key=='Environment' && Value=='ABC']

这里有一些简单的JSON输入 -

{
    "Stacks":  [
        {
            "StackId": "abc123",
            "Tags": [
                {
                    "Value": "Project 1",
                    "Key": "Project"
                },
                {
                    "Value": "ABC",
                    "Key": "Environment"
                }
            ],
            "CreationTime": "2016-07-20T14:49:27.891Z",
            "StackName": "TestStack1",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        },
        {
            "StackId": "xyz123",
            "Tags": [
                {
                    "Value": "Project 1",
                    "Key": "Project"
                },
                {
                    "Value": "XYZ",
                    "Key": "Environment"
                }
            ],
            "CreationTime": "2016-07-20T14:49:27.891Z",
            "StackName": "TestStack2",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        },
        {
            "StackId": "asd123",
            "Tags": [
                {
                    "Value": "Project 2",
                    "Key": "Project"
                },
                {
                    "Value": "ABC",
                    "Key": "Environment"
                }
            ],
            "CreationTime": "2016-07-20T14:49:27.891Z",
            "StackName": "TestStack3",
            "NotificationARNs": [],
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        }
    ]
}

这是我需要的输出 -

{
    "StackId": "asd123",
    "Tags": [
        {
            "Value": "Project 2",
            "Key": "Project"
        },
        {
            "Value": "ABC",
            "Key": "Environment"
        }
    ],
    "CreationTime": "2016-07-20T14:49:27.891Z",
    "StackName": "TestStack3",
    "NotificationARNs": [],
    "StackStatus": "CREATE_COMPLETE",
    "DisableRollback": false
}

1 个答案:

答案 0 :(得分:7)

您可以使用嵌套过滤器:

Stacks[? Tags[? Value=='ABC' && Key=='Environment']]