如何使用jq提取数据

How to extract data using jq

我正在尝试使用 jq 从 JSON 包含特定值的字段中提取数据。

这是我正在使用的JSON:

"results": [
{
  "input": {
    "FUZZ": "actuator",
    "HOST": "https://zzlckzc4kz-3.algolia.net"
  },
  "position": 13,
  "status": 301,
  "length": 162,
  "words": 5,
  "lines": 8,
  "redirectlocation": "https://algolia.net/1/404",
  "resultfile": "",
  "url": "https://zzlckzc4kz-3.algolia.net/actuator"
},
{
  "input": {
    "FUZZ": "actuator/heapdump",
    "HOST": "https://zzlckzc4kz-3.algolia.net"
  },
  "position": 24,
  "status": 301,
  "length": 162,
  "words": 5,
  "lines": 8,
  "redirectlocation": "https://algolia.net/1/404",
  "resultfile": "",
  "url": "https://zzlckzc4kz-3.algolia.net/actuator/heapdump"
},
]

我希望输出为 if "status": 301 then display "url:"

尝试以下操作,假设包含 json 文档的文件位于 /tmp/json:

jq '.results[] | select(.status==301) | .url' /tmp/json

此外,我注意到您在问题中提供的文件实际上不是有效的 JSON 文件。我假设你的意思是:

{
  "results": [
    {
      "input": {
        "FUZZ": "actuator",
        "HOST": "https://zzlckzc4kz-3.algolia.net"
      },
      "position": 13,
      "status": 301,
      "length": 162,
      "words": 5,
      "lines": 8,
      "redirectlocation": "https://algolia.net/1/404",
      "resultfile": "",
      "url": "https://zzlckzc4kz-3.algolia.net/actuator"
    },
    {
      "input": {
        "FUZZ": "actuator/heapdump",
        "HOST": "https://zzlckzc4kz-3.algolia.net"
      },
      "position": 24,
      "status": 301,
      "length": 162,
      "words": 5,
      "lines": 8,
      "redirectlocation": "https://algolia.net/1/404",
      "resultfile": "",
      "url": "https://zzlckzc4kz-3.algolia.net/actuator/heapdump"
    }
  ]
}