这里的一些示例json:

{
  "Tags": [
    {
      "Key": "Name",
      "Value": "foo"
    },
    {
      "Key": "Type",
      "Value": "C"
    }
  ]
}
.

我想仅在“键”为“类型”时打印“值”的值。所以它应该打印出“c”。这就是我到目前为止的目标。

echo $MY_TAGS | jq 'if .Tags[].Key == "Type" then .Tags[].Value else empty end'
.

但它打印出:

“foo”
“c”

有没有办法做到这一点?

有帮助吗?

解决方案

试试:

.Tags[] | select(.Key == "Type") | .Value
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top