Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JQ string cannot be csv-formatted

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
        }
        ]
        }

>Solution :

@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)"'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading