pkaramol
pkaramol

Reputation: 19342

Print specific element of json object of an array using jq

I am describing a AWS security group and passing the output by jq in order to get all the CIDRs of the inbound rules.

I have reached so far:

▶ aws ec2 describe-security-groups --group-ids sg-123456789 | jq '.SecurityGroups[0].IpPermissions[0].IpRanges'
[
  {
    "CidrIp": "11.22.33.44/32",
    "Description": "Something"
  },
  {
    "CidrIp": "22.33.44.12/32",
    "Description": "Something else"
  },
  {
    "CidrIp": "22.11.33.55/32",
    "Description": "Something different"
  },
]

I know I can grep but is there a way to get just the CidrIp from each json element of this array?

Upvotes: 1

Views: 38

Answers (2)

pkaramol
pkaramol

Reputation: 19342

jq '.SecurityGroups[0].IpPermissions[0].IpRanges | values[].CidrIp'

this seemed to work as well.

Upvotes: 0

pmf
pmf

Reputation: 36088

Sure, change your pipeline to

jq -r '.SecurityGroups[0].IpPermissions[0].IpRanges[].CidrIp'

Demo

Note that I also added the -r flag which makes the output raw text instead of JSON.

Upvotes: 1

Related Questions