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

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



我能得到的最接近的结果是选择一个匹配的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
}

最佳答案

您可以使用嵌套过滤器:
Stacks[? Tags[? Value=='ABC' && Key=='Environment']]

关于jmespath - 根据多个键/值对的存在过滤输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41503405/

10-15 18:09