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

Parsing on jq with_entries

I have the following data
Command output:

| jq '.rrsets[]'

{
  "comments": [],
  "name": "name1.",
  "records": [
    {
      "content": "10.10.10.10",
      "disabled": false
    }
  ],
  "ttl": 60,
  "type": "A"
}
{
  "comments": [],
  "name": "name2.",
  "records": [
    {
      "content": "20.20.20.20",
      "disabled": false
    }
  ],
  "ttl": 60,
  "type": "CNAME"
}

I want to get names where type is A.
Help, tell me how to do this?

| jq '.rrsets[] | with_entries(select(.key== "name", .value == "A"))'
{
  "name": "name1."
}
{
  "name": "name2.",
  "type": "A"
}

Displays all the lines, but I only need where type = A

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

>Solution :

I’m not sure if this is what you are looking for, but wouldn’t simply ... | select(.type == "A") do the trick?

... | jq '.rrsets[] | select(.type == "A")'
{
  "comments": [],
  "name": "name1.",
  "records": [
    {
      "content": "10.10.10.10",
      "disabled": false
    }
  ],
  "ttl": 60,
  "type": "A"
}

Demo

And then just get the .name if you want only that (using -r to get rid of the JSON formatting):

... | jq -r '.rrsets[] | select(.type == "A").name'
name1.

Demo

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