【问题标题】:How to extract data using jq如何使用jq提取数据
【发布时间】:2020-02-21 03:39:44
【问题描述】:

我正在尝试使用 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 然后显示 "url:"

【问题讨论】:

    标签: json linux terminal jq conditional-operator


    【解决方案1】:

    假设包含该 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"
        }
      ]
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多