Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally print value based on the value of another key

Tags:

json

jq

Here's some example JSON:

{
  "Tags": [
    {
      "Key": "Name",
      "Value": "foo"
    },
    {
      "Key": "Type",
      "Value": "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'

But it prints out:

"foo"
"C"

Is there a way to do this?

like image 390
stuckintheshuck Avatar asked Jul 28 '14 18:07

stuckintheshuck


1 Answers

Try this:

.Tags[] | select(.Key == "Type") | .Value
like image 84
PeterM Avatar answered Oct 27 '22 03:10

PeterM