Using jq

This documentation is a great example of a Tech Rant – useless.

Examples:

AWS APIs

E.g. a list of AWS Cloud Config Rules.

aws configservice describe-config-rules

outputs

{
    "ConfigRules": [
        {
            "ConfigRuleState": "ACTIVE",
            "Description": "Checks whether ACM Certificates in your account are marked for expiration within the specified number of days. Certificates provided by ACM are automatically renewed. ACM does not automatically renew certificates that you import.",
            "ConfigRuleName": "acm-certificate-expiration-check",
            "ConfigRuleArn": "arn:aws:config:eu-west-1:631413792289:config-rule/config-rule-nlk65s",
            "MaximumExecutionFrequency": "TwentyFour_Hours",
            "Source": {
                "Owner": "AWS",
                "SourceIdentifier": "ACM_CERTIFICATE_EXPIRATION_CHECK"
            },
            "InputParameters": "{\"daysToExpiration\":\"14\"}",
            "Scope": {
                "ComplianceResourceTypes": [
                    "AWS::ACM::Certificate"
                ]
            },
            "ConfigRuleId": "config-rule-nlk65s"
        },
        { etc }, 
        { etc }
    ]
}

https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_manage-rules.html#managing-aws-config-rules-with-the-CLI

Let’s understand the format of the JSON before delving further.

{
    "ConfigRules": [
        {
            "ConfigRuleState": "ACTIVE",
        },
        { etc }, 
        { etc }
    ]
}

Best intro by far is from https://www.w3schools.com/js/js_json_syntax.asp

  • data is in name / value pairs
  • data is separate by commas
  • {} hold objects
  • [] hold arrays

So, we have an object with name = ConfigRules and then a value which is an array of objects.

What we want are the values of those objects where the name is ConfigRuleName.

 

The Manual (https://stedolan.github.io/jq/manual/) says:

Section 3: Basic filters

Array/Object Value Iterator: .[]

which returns all the elements of an array.

TLDR:

aws configservice describe-config-rules | jq '.ConfigRules[].ConfigRuleName'

This helped: https://theagileadmin.com/2017/05/26/aws-cli-queries-and-jq/

 

 

github

Pulling out the commit message and name from a github repo:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

https://stedolan.github.io/jq/tutorial/ 

Note: message:is just the variable name. It could be my_message:. It’s a coincidence that it’s the same as the value in the github commit.

 

Leave a Reply

Your email address will not be published. Required fields are marked *