'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:

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source