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

Linux JQ. How to extract data from specific ID Entry

Guy, long term looking at this board and learning a lot but now stuck with little issue. Im working with Linux shell script that reads json (no problem here). What Im trying to do is get value from entry that has specific Type.

By parsing a json with just jq -r '.', I get

{
  "records": [
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
  ],
  "links": {},
  "meta": {
    "total": 2
  }
}

Then, I use "jq -r '.records' " and get:

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

[
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
]

What I need to do is get data value of the type A. Currently, we have type SOA and A, but I need to only get data from A.

I can use dummy way of "jq -r '.records[1].data'" and it gives me correct response of test.com, but I want a more dynamic way of searching for specific type (in this case "A") and then giving the data value.

Thanks guys!

>Solution :

Is the field called domain_records or just records?

Use select to match your criteria

jq -r '.records[] | select(.type == "A").data'
test.com

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