jq

Tips and tricks for working with the jq command line utility

Install

brew install jq

Example output

aws ec2 describe-security-groups

{
    "SecurityGroups": [
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-0494280510832e7b2",
[snip]  

View All Elements for a Key

  • Returns all the top keys under SecurityGroups

  • If, in this case, there are multiple Security Groups returned the above would return the same Keys multiple times, one set per security group.

  • We can alter the query to only return the Keys for one set of data by adding a 0

View All Elements for a Sub-Key

  • Returns all the top sub-keys under SecurityGroups.IpPermissions

Convert Output into Colorized JSON

  • Makes the returned output pretty JSON and in color.

View Certain Keys

  • Only return data for a specific key.

Key is Exactly

  • Only return data matching the condition, in this case, where GroupId equals "sg-0021f1e76215c0548"

Key Contains

  • Only return data matching the condition, in this case, where IpProtocol contains -1

Return Keys After Filtering

  • Return certain keys for the data matching the condition, in this case, return the GroupName and GroupId for all Security Groups containing a rule allowing all protocols, -1

  • Also, customize the formatting for the output.

Last updated

Was this helpful?