Home > Enterprise >  Selecting multiple values from different nested objects using jq
Selecting multiple values from different nested objects using jq

Time:12-11

I'm working with the AWS cli and I'd like to be able to query instances to get their name and their state. Example:

instance 1, running
instance 2, stopped

I can get the instance name and state with this query

aws ec2 describe-instances --output json --filters Name=tag:Name,Values=*"instance "* --query "Reservations[].Instances[]" | jq ".[] | .Tags, .State.Name"

However, that gives me an array of objects and a single value.

[
  {
    "Key": "Name",
    "Value": "instance 1"
  },
  {
    "Key": "VPC",
    "Value": "the vpc"
  },
  {
    "Key": "ami_backup",
    "Value": "false"
  },
  ...
]
"running"

Almost there. How can I combine each object's Value with the state string so that I can then filter object to only get the one where Key==Name?

I tried using map but couldn't come up with a valid syntax.

CodePudding user response:

Consider:

.[] | (.Tags[] | select(.Key=="Name").Value)   ", "   .State.Name
  • Related