I have the following output of aws ec2 describe-vpcs
:
{
"Vpcs": [
{
"VpcId": "vpc-1f0e197d",
"InstanceTenancy": "default",
"Tags": [
{
"Value": "Product-Production",
"Key": "Name"
}
],
"CidrBlockAssociationSet": [
{
"AssociationId": "vpc-cidr-assoc-f3c5509a",
"CidrBlock": "172.19.0.0/16",
"CidrBlockState": {
"State": "associated"
}
}
],
"State": "available",
"DhcpOptionsId": "dopt-37fd5550",
"CidrBlock": "172.19.0.0/16",
"IsDefault": false
},
{
another vpc...
}
]
}
I'm using jq
to catch the "VpcId" if
.Tags[].Value==Product-Production
but no matter what I try, I can't get the right syntax, how can it be achieved?
You are so-close but not quite the right filter. You need to use a select
expression here to match the object matching your string condition and filter the value out of it.
jq '.Vpcs[] | select( .Tags[].Value| contains("Product-Production")) | .VpcId'
If you break down the filter,
.Vpcs[]
lists all the elements within the array on which we apply the condition..Value
contains the string you need here. So at the end of first pipe-line output you get the actual element from the list of the elements in the array, matching the condition defined..VpcId
with the last pipeline.If you haven't used jqplay.org before, you should try it and get your filters into action and work it out online.