LinuxJQ。如何从特定 ID 条目中提取数据

Linux JQ. How to extract data from specific ID Entry

伙计,长期关注此板并学到了很多东西,但现在几乎没有遇到什么问题。我正在使用读取 json 的 Linux shell 脚本(这里没问题)。我想做的是从具有特定类型的条目中获取价值。

通过仅用 jq -r '.' 解析 json,我得到

{
  "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
  }
}

然后,我使用 "jq -r '.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
  }
]

我需要做的是获取类型A的数据值。目前我们有类型SOA和A,但我只需要从A中获取数据。

我可以使用 "jq -r '.records[1].data'" 的虚拟方式,它给了我 test.com 的正确响应,但我想要一种更动态的方式来搜索特定类型(在本例中为“A”)和然后给出数据值。

谢谢大家!

字段名称是 domain_records 还是 records

使用 select 来匹配您的条件

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

Demo