Bhavik Joshi
Bhavik Joshi

Reputation: 2677

How to remove extra parentheses and angular brackets while using aws cli?

I am trying to get the info using of NetworkInterface using following command.

[root@ip-172-29-45-82 ~]# aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[*].{VpcId:Instances[*].NetworkInterfaces[*].VpcId}
[
    {
        "VpcId": [
            [
                "vpc-38fb075d"
            ]
        ]
    }
]

I don't want extra parentheses and angular brackets.

It should be something like as follows:

["VpcId":"vpc-38fb075d"] or [{"VpcId":"vpc-38fb075d"}] or {"VpcId":"vpc-38fb075d"}

Is there any way to achieve above output from the above command.

Thanks in advance.

Upvotes: 5

Views: 3005

Answers (3)

Humza Javaid
Humza Javaid

Reputation: 81

You could run - given that you can't have ENIs in different VPCs:

aws ec2 describe-instances --query 'Reservations[].Instances[].VpcId' --output text --instance-ids i-dd6f6f53

You can also try nesting your JSON objects - for example, trying to get the name tag from the parent bracket Tags:

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,PrivateIpAddress, [Tags[?Key==`Name`].Value][0][0]]' --output text

It is easier to develop the query using the JSON output, then cleaning things up with --output text.

Upvotes: 4

Hua2308
Hua2308

Reputation: 469

Result can be further refined by adding this to the end of your command

--output text

And the output will be unstructured text:

"VpcId": "vpc-38fb075d"

Upvotes: 6

Bhavik Joshi
Bhavik Joshi

Reputation: 2677

I solved it.

I used following command.

aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[0].{VpcId:Instances[0].NetworkInterfaces[0].VpcId}

I just changed * to 0 and its working.

Now the output is

{
    "VpcId": "vpc-38fb075d"
}

Upvotes: 5

Related Questions