Shairon Toledo Shairon Toledo - 10 days ago 7
JSON Question

How to get a flat output based on conditional jq query?

I have the following JSON:

[
{
"name": "InstanceA",
"tags": [
{
"key": "environment",
"value": "production"
},
{
"key": "group",
"value": "group1"
}
]
},
{
"name": "InstanceB",
"tags": [
{
"key": "group",
"value": "group2"
},
{
"key": "environment",
"value": "staging"
}
]
}
]


I'm trying to get a flat output of
value
based on the condition
key == 'environment'
. I already tried
select(boolean_expression)
, but I cannot get the desired output, like:


"InstanceA, production"
"InstanceB, staging"


Does
jq
support this kind of output? If so, how to do it?

Answer
jq '.[] | .name + ", " + (.tags[] | select(.key == "environment").value)' f.json
Comments