简体   繁体   中英

How to extract data using jq

I am trying to use jq to extract data from JSON fields that contain a specific value.

This is the JSON that I'm working with:

"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"
},
]

i want the output to be if "status": 301 then display "url:"

Try the following, assuming the file containing that json document is at /tmp/json :

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

Also, I notice the document you supplied in the question is not actually a valid JSON document. I assume you meant the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM