值对的存在过滤输出

值对的存在过滤输出

本文介绍了根据多个键/值对的存在过滤输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using JMESPath, is it possible to filter output based on the existence of multiple key/value pairs within the input?

从下面的示例JSON中,我想做的是仅提取Tags-

From the example JSON below, what I'd like to do is extract only the objects that contain these key/value pairs within Tags -

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

The closest I can get is to select a single matching Tag, but I require the rest of the object too, and I also need to match against the other key/value pair -

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

以下是一些示例JSON输入-

Here is some exaple JSON input -

{
    "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
}

推荐答案

您可以使用嵌套过滤器:

You can use nested filters:

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

这篇关于根据多个键/值对的存在过滤输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:53