stuckintheshuck
stuckintheshuck

Reputation: 2529

Conditionally print value based on the value of another key

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?

Upvotes: 10

Views: 4646

Answers (1)

PeterM
PeterM

Reputation: 1198

Try this:

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

Upvotes: 17

Related Questions