Home > Mobile >  How to parse aws cli output using jq
How to parse aws cli output using jq

Time:08-14

aws elbv2 describe-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

provides

{
    "Attributes": [
        {
            "Value": "false",
            "Key": "stickiness.enabled"
        },
        {
            "Value": "300",
            "Key": "deregistration_delay.timeout_seconds"
        },
        {
            "Value": "lb_cookie",
            "Key": "stickiness.type"
        },
        {
            "Value": "86400",
            "Key": "stickiness.lb_cookie.duration_seconds"
        },
        {
            "Value": "0",
            "Key": "slow_start.duration_seconds"
        }
    ]
}

I would like to fetch deregistration_delay.timeout_seconds from the output

I tried which works for this case when deregistration_delay.timeout_seconds appears on the second position.

aws elbv2 describe-target-group-attributes \
        --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067
 | jq -r '.Attributes[1].Value'

but for some target groups the deregistration_delay.timeout_seconds is placed at a different number.

How can I use jq to fetch deregistration_delay.timeout_seconds

CodePudding user response:

You can actually use JMESPATH in the AWS CLI without needing to use jq:

aws elbv2 describe-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \
    --query "Attributes[?Key=='deregistration_delay.timeout_seconds']|[0].Value" \
    --output text

JMESPATH was created by James Saryerwinnie, one of the authors of the AWS CLI. The tutorial is well worth reading.

  • Related