如何过滤嵌套数组中不包含键值对的条目

How can I filter for entries that do NOT contain a key-value pair within a nested array

假设我有以下 JSON 输出:

{
 "Stacks": [
        {
            "StackName": "hello-world",
            "Tags": [
                {
                    "Key": "environment",
                    "Value": "sandbox"
                },
                {
                    "Key": "Joe Shmo",
                    "Value": "Dev"
                }
            ]
        },
        {
            "StackName": "hello-man",
            "Tags": [
                {
                    "Key": "environment",
                    "Value": "live"
                },
                {
                    "Key": "Tandy",
                    "Value": "Dev"
                }
            ]
        }
    ]
}

我将如何编写一个 jq 查询来获取所有 StackName 的堆栈 NOT 具有 Tags"Key": "Joe Shmo"?所以结果 return 只是 hello-man.

使用 contains,像这样:

jq -r '.Stacks[]|select(.Tags|contains([{"Key": "Joe Shmo"}])|not).StackName'

注意:-r 从输出中删除引号,否则 jq 将打印 "hello-man"(在双引号内)

.Stacks[]
| select( any(.Tags[]; .Key == "Joe Shmo" ) | not)
| .StackName

这会有效地检查是否相等(any 具有短路语义),而 contains 会检查是否包含。