Awesim
Awesim

Reputation: 89

Using JQ to grep boolean and array at the same time

I'm pretty new with Jq and have some trouble starting.

I am making a new bash script that gets a .json file from URL and it needs to grep multiple values but save only domains to a new file as output.

In this example, I would need to grep all companies with identity==true, but save only the domains from the array that matches identity=true to a new file.

I have tried almost everything but to no luck.

Example JSON:

{
    "companies": [
        {
            "name": "CompanyOne",
            "url": "https://companyone.com",
            "identity": false,
            "domains": [
                "companyone.com"
            ]
        },
        {
            "name": "CompanyTwo",
            "url": "https://companytwotwo.com",
            "identity": true,
            "domains": [
                "companytwo.com",
                "companytwotwo.net"
            ]
        }
    ]
}

Desired output:

companytwo.com
companytwotwo.net

Upvotes: 0

Views: 727

Answers (1)

x4k3p
x4k3p

Reputation: 1769

Well, just use select like this:

cat data.json | jq -r '.companies[] | select(.identity) | .domains[]' > yourfile.txt

Try it out https://jqplay.org/s/NKD3-BkXLj

Upvotes: 1

Related Questions