Home > Net >  Print specific element of json object of an array using jq
Print specific element of json object of an array using jq

Time:01-06

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?

CodePudding user response:

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.

CodePudding user response:

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

this seemed to work as well.

  •  Tags:  
  • Related