Home > Enterprise >  Linux JQ. How to extract data from specific ID Entry
Linux JQ. How to extract data from specific ID Entry

Time:03-05

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:

[
  {
    "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!

CodePudding user response:

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

  • Related