Parsing JSON data from a curl API request

13,145

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

Share:
13,145

Related videos on Youtube

Ryan R
Author by

Ryan R

Studying to become a Linux System Admin

Updated on September 18, 2022

Comments

  • Ryan R
    Ryan R over 1 year

    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"
    
  • Ryan R
    Ryan R over 4 years
    First solution worked, had to remove the -i on the curl. Thanks!