Search code examples
jsontagsexport-to-csvjqaws-cli

How to filter json file with specific tag values and get output into csv


I have created a json file with the output having key values pair. But I would like to filter more and get only specific tags and get new output in table using excel (csv) format

aws resourcegroupstaggingapi get-resources --tags-per-page 100 --tag-filters Key=ProjectName,Values=Avengers > tag-filter.json

However it provides the list of all the tags besides "ProjectName". I would like to filter the output with 2 more tags with their values but not all of them:

Actual results:

{
    "ResourceTagMappingList": [
        {
            "ResourceARN": "arn:aws:app:us-east-1:XXXX/mesh/Avenger1",
            "Tags": [
                {
                    "Key": "ApplicationName",
                    "Value": "HULK"
                },
                {
                    "Key": "Owner",
                    "Value": "Mark Ruffalo"
                },
                {
                    "Key": "Costume",
                    "Value": "GREEN"
                },
                {
                    "Key": "Power",
                    "Value": "SMASH"
                },
                {
                    "Key": "ProjectName",
                    "Value": "Avengers"
                }
            ]
        },
        {
            "ResourceARN": "arn:aws:app:us-east-1:XXXX:mesh/Avenger2",
            "Tags": [
                {
                    "Key": "ApplicationName",
                    "Value": "IRON-MAN"
                },
                {
                    "Key": "Owner",
                    "Value": "Robert Downey Jr."
                },
                {
                    "Key": "Costume",
                    "Value": "RED"
                },
                {
                    "Key": "Power",
                    "Value": "SuperSonic"
                },
                {
                    "Key": "ProjectName",
                    "Value": "Avengers"
                }
            ]
        }
    ]
}   

Expected Results:

{
    "ResourceTagMappingList": [
        {
            "ResourceARN": "arn:aws:app:us-east-1:XXXX/mesh/Avenger1",
            "Tags": [
                {
                    "Key": "ApplicationName",
                    "Value": "HULK"
                },
                {
                    "Key": "Owner",
                    "Value": "Mark Ruffalo"
                },
                {
                    "Key": "ProjectName",
                    "Value": "Avengers"
                }
            ]
        },
        {
            "ResourceARN": "arn:aws:app:us-east-1:XXXX:mesh/Avenger2",
            "Tags": [
                {
                    "Key": "ApplicationName",
                    "Value": "IRON-MAN"
                },
                {
                    "Key": "Owner",
                    "Value": "Robert Downey Jr."
                },
                {
                    "Key": "ProjectName",
                    "Value": "Avengers"
                }
            ]
        }
    ]
}

Solution

  • To achieve the "expected" output given the "actual" output, you could use the following filter:

    .ResourceTagMappingList[].Tags
     |= map(select(.Key|IN("ApplicationName","Owner","ProjectName")))
    

    To achieve the expected CSV, it would be helpful to know what you expect.