Home > front end >  JQ - Remove Duplicate Array Values
JQ - Remove Duplicate Array Values

Time:07-06

I am using the following jq query to extract the AWS ARN and associated protocols. However I only need the ARN to be listed once followed by the ports and protocols

my code is jq -r '.Listeners[] | .LoadBalancerArn' and the results are

"arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde"
"arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde"
"arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde"

I have tried everything including unique, first, unique_by, select, contains, etc.. and the results are always "Cannot iterate over string" or number

Sample JSON

{
    "Listeners": [
        {        
            "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde",
            "Port": 9090,
            "Protocol": "HTTP"
        },
        {        
            "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde",
            "Port": 80,
            "Protocol": "HTTP"
            },
        {       
            "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-xxxx-1:123456789:loadbalancer/app/msword-123456789/20b73abcde",
            "Port": 443,
            "Protocol": "HTTPS"
        }
    ]
}

CodePudding user response:

unique works on an array, so you'll need to create one with all the LoadBalancerArn first, then call unique and get the first of the remaining array:

.Listeners | map(.LoadBalancerArn) | unique | first

CodePudding user response:

Does this produce what you expected ?

jq -r '.Listeners |
      group_by(.LoadBalancerArn)[] |
      first |
      "\(.LoadBalancerArn) \(.Protocol)"
      ' input.json
  • Related