1

I'm using Shodan's API https://developer.shodan.io/api to get my current network alerts. I want to parse out the id for the alerts with jq.

The curl request is curl -X GET -i https://api.shodan.io/shodan/alert/info?key={API KEY}

The output of that request is json data fomatted like so:

[
 {
  "name": "Test Alert",
  "created": "2017-01-09T21:53:17.104000",
  "expires": 0,
  "expiration": null,
  "filters": {
   "ip": [
    "198.20.88.870"
   ]
  },
  "id": "HKVGCP1WD79Z7W2T",
  "size": 1
 }
]

Using curl -X GET -i https://api.shodan.io/shodan/alert/info?key={API KEY} | jq '.id' give the following error:

"parse error: Invalid numeric literal at line 1, column 9"
7

The -i option means curl will include http response headers which are not in JSON format. That is what causes your parse error, however given the json you provided, you need to use [] to tell it to iterate the array:

curl 'https://api.shodan.io/shodan/alert/info?key={API KEY}' | jq '.[].id'

alternatively (and IMO more intuitively) using json:

curl 'https://api.shodan.io/shodan/alert/info?key={API KEY}' | json -a id

Additionally json(1) has the -H option to ignore http response headers so you could use json -Ha id

  • First solution worked, had to remove the -i on the curl. Thanks! – Ryan R Jan 18 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.