根据另一个键的值有条件地打印值 [英] Conditionally print value based on the value of another key

查看:64
本文介绍了根据另一个键的值有条件地打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些JSON示例:

Here's some example JSON:

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

我只想在键"为类型"时打印值"的值.因此,它应该打印出"C".到目前为止,这就是我所拥有的.

I want to print the value of "Value" only when "Key" is "Type". So it should print out "C". This is what I have so far.

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

但它会打印出来:

"foo"
"C"

"foo"
"C"

有没有办法做到这一点?

Is there a way to do this?

推荐答案

尝试一下:

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

这篇关于根据另一个键的值有条件地打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆