根据另一个值从JSON提取值

时间:2020-07-15 03:22:46

标签: json grep jq

我得到了json格式的结果,如下所示。我使用jq解析了相应的文本文件。

cat file.txt | jq

结果

{
  "results": [
    {
      "input": {
        "FUZZ": "fuzzerhere"
      },
      "position": 53,
      "status": 403,
      "length": 670,
      "words": 105,
      "lines": 9,
      "redirectlocation": "",
      "resultfile": "",
      "url": "https://www.example.com/fuzzerhere",
      "host": "www.example.com"
    },
    {
      "input": {
        "FUZZ": "newfuzz"
      },
      "position": 53,
      "status": 200,
      "length": 670,
      "words": 105,
      "lines": 9,
      "redirectlocation": "",
      "resultfile": "",
      "url": "https://www.example.com/newfuzz",
      "host": "www.example.com"
    }
  ]
}

要求:我想grep或从满足条件的输出中提取。
条件:状态== 200
输出:相应的输出应包含状态为200的所有url,考虑以下示例,该输出必须为 https://www.example.com/newfuzz

我尝试使用grep cat file.txt | jq | grep '"status": 403'

上面的命令仅给出了行状态,然后我意识到从json对象数组中提取内容是不同的。

1 个答案:

答案 0 :(得分:2)

您可以使用jq完成所有操作:

jq -r '.results[] | select(.status == 200).url'

urlstatus的所有数组元素返回200字段。