I'm using jq to try to parse some details from an AWS CLI query.
Example JSON:
"Tags": [
{
"Key": "Name",
"Value": "db01"
},
{
"Key": "BackupResourceType",
"Value": "EC2"
},
{
"Key": "Role",
"Value": "db_edi01"
},
{
"Key": "OS",
"Value": "Ubuntu 18.04"
}
],
The issue I'm running into is these tag values aren't in a consistent order. Is there a way I can select the value where key=OS instead of selecting the specific array value?
[.InstanceId, .State.Name, .Tags[1].Value, .Tags[0].Value, etc]
Thanks for the help!
Perhaps the niftiest way would be to use from_entries
. E.g. for the "OS" key:
.Tags | from_entries | .OS
More generally, but more verbosely, you could go with:
.Tags | first( .[] | select(.Key == "OS") | .Value)
or if you're concerned about the result if the specified key is not present:
.Tags | first( .[] | select(.Key == "OS") // {} | .Value)
Without seeing a complete sample, it's hard to say exactly what the most economical expression would be, but piggy-backing off your example, you could go with something like:
[.InstanceId, .State.Name] +
[.Tags | from_entries | (.OS, .Role)]