0

I want to convert a json file to csv file.

I have a script:

 cat convert.sh
INPUTFILE="test.json"
OUTPUTFILE="outputfile-formatted.csv"
jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' ${INPUTFILE} > ${OUTPUTFILE}

but I got: jq: error (at test.json:136): Cannot index object with number

my json is:

{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "SG for xxxsubnets",
            "Tags": [
                {
                    "Value": "SG-xxx",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "Description": "xxxt subnets",
                            "CidrIp": "xxx.3.0/27"
                        },
                        {
                            "Description": "xxx subnets",
                            "CidrIp": "xx4.3.32/27"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                },
                {
                    "PrefixListIds": [],
                    "FromPort": 8443,
                    "IpRanges": [
                        {
                            "CidrIp": "xxxx2.0/23"
                        }
                    ],
                    "ToPort": 8443,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "SG-xxxxt",
            "VpcId": "vpc-xxxxf4168bfbb5f",
            "OwnerId": "xxx43024",
            "GroupId": "sg-xxx722710ba294"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "launch-wizard-1 created 2020-01-24T07:50:38.598+01:00",
            "IpPermissions": [
                {
                    "PrefixListIds": [],
                    "FromPort": 22,
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "launch-wizard-1",
            "VpcId": "vpc-0xxxx4168bfbb5f",
            "OwnerId": "xxxxx043024",
            "GroupId": "sg-xxx00230c6c48ec"
        },
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": [],
                    "Ipv6Ranges": []
                }
            ],
            "Description": "default VPC security group",
            "Tags": [
                {
                    "Value": "SG-default",
                    "Key": "Name"
                }
            ],
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "PrefixListIds": [],
                    "IpRanges": [],
                    "UserIdGroupPairs": [
                        {
                            "UserId": "xxxx43024",
                            "GroupId": "sg-xxxxa62ca8a07c3"
                        }
                    ],
                    "Ipv6Ranges": []
                }
            ],
            "GroupName": "default",
            "VpcId": "vpc-xxxx4df4168bfbb5f",
            "OwnerId": "xxxx14043024",
            "GroupId": "sg-xxx2aa62ca8a07c3"
        }
    ]
}

then I have tried to modify the script with:

INPUTFILE="test.json"
OUTPUTFILE="outputfile-formatted.csv"
jq -r '(".[0]" | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' ${INPUTFILE} > ${OUTPUTFILE}

but failed again:

jq: error (at test.json:136): string (".[0]") has no keys
Roger
  • 1

0 Answers0