I'm trying to use the following jq command to return a true output, if any of one of the condition is true in the list.
.Tags[] as $t| "aws:cloudformation:stack-name"| IN($t[])
Input
{
"Tags": [{
"Value": "INF-D-XX-SEC-OPNV-UW1",
"Key": "Name"
},
{
"Value": "INF-D-XX-CFS-StandardInfrastructure-UW1",
"Key": "aws:cloudformation:stack-name"
},
{
"Value": "sgOpenVPNAccess",
"Key": "aws:cloudformation:logical-id"
},
{
"Value": "UW1",
"Key": "Location"
},
{
"Value": "INF",
"Key": "Application"
},
{
"Value": "D",
"Key": "Parent Environment"
},
{
"Value": "arn:aws:cloudformation:us-west-1:111111:stack/INF-D-XX-CFS-StandardInfrastructure-UW1/1111-11-11e8-96fe-11",
"Key": "aws:cloudformation:stack-id"
},
{
"Value": "OPNV",
"Key": "ResourceType"
}
]
}
This gave me a list of returned boolean values back as the following,
--output--
true
false
false
false
false
false
false
I would like to return a single value true
if one of the
Key="aws:cloudformation:stack-name"
is detected and without given me a list of value back.
A very efficient solution (both with respect to time and space) is easy thanks to any/2
:
any(.Tags[]; .Key == "aws:cloudformation:stack-name")
This of course evaluates either to true
or false
. If you want true
or nothing at all, you could tack on // empty
to the above.
Building on the previous answer by @peak, as can't post comments, you can use the '-e' flag to jq to set the exit status so you can easily chain shell commands together. This avoids having to test for the returned string.
jq -e 'any(.Tags[]; .Key == "aws:cloudformation:stack-name")' json >/dev/null && echo 'Exists' || echo 'Missing'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With