2

My goal is to parse the result of www.worldtides.info. I have a Raspberry Pi 2 and I'm scripting with Linux.

I have an API key and the curl request is something like:

curl -s "http://www.worldtides.info/api?extremes&lat=my_latitude&lon=my_longitude&key=my_api_key"

Which gets a result like this:

{
  "status": 200,
  "requestLat": "my_latitude",
  "requestLon": "my_longitude",
  "extremes": [
    {
      "dt": 1459680132,
      "date": "2016-04-03T10:42+0000",
      "height": 0.7343567325036922,
      "type": "High"
    },
    {
      "dt": 1459702028,
      "date": "2016-04-03T16:47+0000",
      "height": -0.8438121322770741,
      "type": "Low"
    },
    {
      "dt": 1459724478,
      "date": "2016-04-03T23:01+0000",
      "height": 1.0419712550773803,
      "type": "High"
    },
    {
      "dt": 1459747135,
      "date": "2016-04-04T05:18+0000",
      "height": -1.1049607153344834,
      "type": "Low"
    },
    {
      "dt": 1459769354,
      "date": "2016-04-04T11:29+0000",
      "height": 1.1012796430343657,
      "type": "High"
    },
    {
      "dt": 1459791366,
      "date": "2016-04-04T17:36+0000",
      "height": -1.204313872235808,
      "type": "Low"
    },
    {
      "dt": 1459813613,
      "date": "2016-04-04T23:46+0000",
      "height": 1.3452661348073778,
      "type": "High"
    },
    {
      "dt": 1459835931,
      "date": "2016-04-05T05:58+0000",
      "height": -1.4062688322894952,
      "type": "Low"
    }
  ]
}

Using jq, I want to get the times of high and low tides for the current day and tomorrow, but when I try this:

.result[].type

But it gives me an error:

jq: error (at <stdin>:0): Cannot iterate over null (null)`

I know I can get the type of tide it is using this:

.extremes[].type

the result is:

High
Low
etc...

and the time of the tide using this:

.extremes[].date

the result is:

2016-04-03T10:42+0000
2016-04-03T16:47+0000
etc...

So, how do I combine the results together to get an output like this?

2016-04-03T10:42+0000 High
2016-04-03T16:47+0000 Low
etc...
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
pyrrhus
  • 57
  • 1
  • 8

1 Answers1

1

Something along the lines of

curl -s "http://www.worldtides.info/api?extremes&lat=my_latitude&lon=my_longitude&key=my‌​_api_key"| jq -r ".extremes[] | .date + .type"

Should give you both the date/time and High/Low tide output for each entry.


Basically, you need to use extremes NOT result. There is no result key, hence jq is giving you messages about null.

Tersosauros
  • 883
  • 1
  • 12
  • 22