有条件地根据另一个键的值打印值

stu*_*uck 6 json jq

这是一些JSON示例:

{
  "Tags": [
    {
      "Key": "Name",
      "Value": "foo"
    },
    {
      "Key": "Type",
      "Value": "C"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我想仅在"Key"为"Type"时才打印"Value"的值.所以它应该打印出"C".这就是我到目前为止所拥有的.

echo $MY_TAGS | jq 'if .Tags[].Key == "Type" then .Tags[].Value else empty end'
Run Code Online (Sandbox Code Playgroud)

但它打印出来:

"foo"
"C"

有没有办法做到这一点?

Pet*_*erM 11

试试这个:

.Tags[] | select(.Key == "Type") | .Value
Run Code Online (Sandbox Code Playgroud)