JQ makes me irrationally angry and I despise its very existence. I have been trying to extract the value of the tf-name
key for 30 minutes and I'm going to become a hermit if I have to make another attempt at this. Please, how do I get tmp
as a a result?
➜ aws resourcegroupstaggingapi get-resources --tag-filters Key=tf-name,Values=tmp --profile=test | jq
{
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:s3:::stack-hate-1234567890salt",
"Tags": [
{
"Key": "aws:cloudformation:stack-name",
"Value": "hatestack"
},
{
"Key": "aws:cloudformation:stack-id",
"Value": "arn:aws:cloudformation:us-west-2:6666666666666:stack/FSStack/hate-hate-hate-123456789"
},
{
"Key": "tf-name",
"Value": "tmp"
},
{
"Key": "aws:cloudformation:logical-id",
"Value": "hatebucket1234"
},
{
"Key": "aws-cdk:auto-delete-objects",
"Value": "true"
}
]
}
]
CodePudding user response:
Its quite simple to extract, once you know the type of the top level objects. You can see ResourceTagMappingList
is a list of records, so it should have a []
notation following it, which I think most beginners with jq
tend to miss out. So
.ResourceTagMappingList[].Tags | from_entries."tf-name"
should get you the desired value. See jqplay demo
The way it works is, the from_entries
function takes an array of objects with key names key
and value
and transforms it to a value only pair. From there, we extract only the key name tf-name
. The special double quotes are needed because the key name here contains a meta-character -
, which should be quoted to treat the whole string as the key name.
Another way would be to use a select
statement like below
.ResourceTagMappingList[].Tags[] | select(.Key == "tf-name").Value