Home > Mobile >  JQ string cannot be csv-formatted
JQ string cannot be csv-formatted

Time:06-29

I am working with JSON output from the AWS cmd aws elbv2 describe-load-balancers --output json to retrieve info about my Load Balancers - partial json below

I am using the following to extract DNS Name, LB Name and Scheme for each LB

jq -r ' .LoadBalancers[]| .DNSName,.LoadBalancerName,.Scheme'

The JQ loops over each array and I get a list like such

DNS-XXXXXXXXXXXXXXXXXXXXXXXX.elb.us-west-1.amazonaws.com
LBNAME-xxxxxxxxx
internal
DNS-XXXXXXXXXX.elb.us-west-1.amazonaws.com
LBNAME-XXXXXXXXXXXXXXXXXXXXXXXX
internal

QUESTION: Is there a way in JQ to print my values as a CSV or at least insert a space after every iteration of the loop ? If I can't do it via JQ what is the best way to achieve this ?

When I add @csv I get the following error "string ("XXXX...) cannot be csv-formatted, only array"

Partial JSON

"LoadBalancers": [
        {
            "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-1:XXXXXXXXXXXXXXXXXXXXXXXX,
            "DNSName": "DNS-XXXXXXXXXXXXXXXXXXXXXXXX.elb.us-west-1.amazonaws.com",
            "CanonicalHostedZoneId": "xxxxxxxxx",
            "CreatedTime": "2021-10-24T07:37:29.568000 00:00",
            "LoadBalancerName": "LBNAME-xxxxxxxxx",
            "Scheme": "internal",
            "VpcId": "vpc-XXXXXXXXXX",
            "State": {
                "Code": "active"
            },
            "Type": "network",
            "AvailabilityZones": [
                {
                    "ZoneName": "us-west-1a",
                    "SubnetId": "XXXXXXXXXX,
                    "LoadBalancerAddresses": []
                },
                {
                    "ZoneName": "us-west-1c",
                    "SubnetId": "XXXXXXXXXX",
                    "LoadBalancerAddresses": []
                }
            ],
            "IpAddressType": "ipv4"
        },
        {
            "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-1:XXXXXXXXXXXXXXXXXXXXXXXX,
            "DNSName": "DNS-XXXXXXXXXX.elb.us-west-1.amazonaws.com",
            "CanonicalHostedZoneId": "XXXXXXXXXX",
            "CreatedTime": "2022-01-05T14:14:14.570000 00:00",
            "LoadBalancerName": "LBNAME-XXXXXXXXXXXXXXXXXXXXXXXX",
            "Scheme": "internal",
            "VpcId": "vpc-XXXXXXXXXX",
            "State": {
                "Code": "active"
            },
            "Type": "network",
            "AvailabilityZones": [
                {
                    "ZoneName": "us-west-1c",
                    "SubnetId": "subnet-XXXXXXXXXX",
                    "LoadBalancerAddresses": []
                },
                {
                    "ZoneName": "us-west-1a",
                    "SubnetId": "subnet-ZZZZZZZ",
                    "LoadBalancerAddresses": []
                }
            ],
            "IpAddressType": "ipv4"
        }
        ]
        }

CodePudding user response:

@csv expects an array, so construct one with your desired fields in it:

jq -r '.LoadBalancers[] | [.DNSName, .LoadBalancerName, .Scheme] | @csv'

Alternatively, construct each output line yourself by formatting a string to your likings:

jq -r '.LoadBalancers[] | "\(.DNSName): \(.LoadBalancerName), \(.Scheme)"'
  • Related