如何使用 jq 数组值返回 true

Fan*_*ang 4 arrays json any jq

如果列表中的任何一个条件为 true,我尝试使用以下 jq 命令返回 true 输出。

.Tags[] as $t| "aws:cloudformation:stack-name"| IN($t[])   
Run Code Online (Sandbox Code Playgroud)

输入

 {
    "Tags": [{
            "Value": "INF-D-XX-SEC-OPNV-UW1",
            "Key": "Name"
        },
        {
            "Value": "INF-D-XX-CFS-StandardInfrastructure-UW1",
            "Key": "aws:cloudformation:stack-name"
        },
        {
            "Value": "sgOpenVPNAccess",
            "Key": "aws:cloudformation:logical-id"
        },
        {
            "Value": "UW1",
            "Key": "Location"
        },
        {
            "Value": "INF",
            "Key": "Application"
        },
        {
            "Value": "D",
            "Key": "Parent Environment"
        },
        {
            "Value": "arn:aws:cloudformation:us-west-1:111111:stack/INF-D-XX-CFS-StandardInfrastructure-UW1/1111-11-11e8-96fe-11",
            "Key": "aws:cloudformation:stack-id"
        },
        {
            "Value": "OPNV",
            "Key": "ResourceType"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个返回布尔值的列表,如下所示,

- 输出 -

true
false
false
false
false
false
false
Run Code Online (Sandbox Code Playgroud)

true如果其中之一,我想返回一个值

Key="aws:cloudformation:stack-name" 
Run Code Online (Sandbox Code Playgroud)

被检测到并且没有给我返回值列表。

pea*_*eak 6

一个非常有效的解决方案(在时间和空间方面)很容易,这要归功于any/2

any(.Tags[]; .Key == "aws:cloudformation:stack-name")
Run Code Online (Sandbox Code Playgroud)

这当然评估为truefalse。如果你想要true或者什么都不想要,你可以继续// empty上面的内容。