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

Use jq to extract sublist into a single line

From,

jq '.DistributionList.Items[] | select(.Aliases.Items != null) | .Id + "," + .DomainName' << EOF
{
"DistributionList": {
    "Items": [
        {
        "Id": "EG3MOA",
        "Status": "Deployed",
        "LastModifiedTime": "2022-12-03T19:32:35.007000+00:00",
        "DomainName": "a***.cloudfront.net",
            "Aliases": {
                "Quantity": 1,
                "Items": [
                "a.domain.tld",
                "b.domain.tld"
                ]
            }
        },
        {
        "Id": "EG3MOB",
        "Status": "Deployed",
        "LastModifiedTime": "2022-12-03T19:32:35.007000+00:00",
        "DomainName": "b***.cloudfront.net",
            "Aliases": {
                "Quantity": 1,
                "Items": [
                "c.domain.tld",
                "d.domain.tld"
                ]
            }
        }
    ]
    }
}
EOF

It yields:

"EG3MOA,a***.cloudfront.net"
"EG3MOB,b***.cloudfront.net"

How would I also get the `Alias Items, so that I have:

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

"EG3MOA,a***.cloudfront.net,'a.domain.tld,b.domain.tld'"
"EG3MOB,b***.cloudfront.net,'c.domain.tld,d.domain.tld'"

>Solution :

To extract the Aliases.Items array and combine it into a single string, you can use the join() method in jq. For example, this jq filter would extract the Id, DomainName, and Aliases.Items values and combine them into a single string, with the Aliases.Items values joined together with a comma:

jq '.DistributionList.Items[] | select(.Aliases.Items != null) | .Id + "," + .DomainName + ",'" + (.Aliases.Items | join(",")) + "'"

Here’s an example of how you could use this filter in your command:

jq '.DistributionList.Items[] | select(.Aliases.Items != null) | .Id + "," + .DomainName + ",'" + (.Aliases.Items | join(",")) + "'" << EOF
{
"DistributionList": {
    "Items": [
        {
        "Id": "EG3MOA",
        "Status": "Deployed",
        "LastModifiedTime": "2022-12-03T19:32:35.007000+00:00",
        "DomainName": "a***.cloudfront.net",
            "Aliases": {
                "Quantity": 1,
                "Items": [
                "a.domain.tld",
                "b.domain.tld"
                ]
            }
        },
        {
        "Id": "EG3MOB",
        "Status": "Deployed",
        "LastModifiedTime": "2022-12-03T19:32:35.007000+00:00",
        "DomainName": "b***.cloudfront.net",
            "Aliases": {
                "Quantity": 1,
                "Items": [
                "c.domain.tld",
                "d.domain.tld"
                ]
            }
        }
    ]
    }
}
EOF

This should output the following:

"EG3MOA,a***.cloudfront.net,'a.domain.tld,b.domain.tld'"
"EG3MOB,b***.cloudfront.net,'c.domain.tld,d.domain.tld'"
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