Using jq and curl to check the weather
Mon Oct 28, 2019 · 405 words · 3 min

Have you ever wondered what the weather was like, but not wondered enough to run some web app that takes 120 seconds to load a bunch of tracking javascript and ads, let alone actually go outside. In this post I'll show you what is in my opinion the best way to get your weather report. First though, we need some tools.

jq

From the man page, jq is a "Command-line JSON processor." Actually, jq is much more powerful. It is a compact programming language that uses JSON as its main data structure.

Here is a taste of jq. In this example, the first (and only) argument is the program to be run.

$ jq 'sort_by(.bday) | map(.name) | join(", ")' \
<<EOF
[
  { "name": "George", "bday": 1562118988 },
  { "name": "John", "bday": 1268818921 },
  { "name": "Paul", "bday": 1362818918 }
]
EOF
"John, Paul, George"

This example program takes its input, sorts it on the key bday, maps each element to the key name, and joins the resulting array with ', '.

curl

Okay, now that we can process our data, let's get some. Luckily, our old friend curl can help us out. For this demo, we will get our data from darksky, if you want to follow along, you need to get an API key.

To avoid repeating myself, I will store the API endpoint in a variable.

$ api="https://api.darksky.net/forecast/<key>/33.5206824,-86.8024326"

Now if we just point curl at the endpoint, we get some JSON.

$ curl -sS "$api"
{"latitude":33.5206824,"longitude":-86.8024326,"timezone":"America/Chicago"...

Now just add a dash of jq, and voilĂ , the weather.

$ curl -sS "$api" | jq -Mr '.currently.summary'
Mostly Cloudy

If you want a fancier output, you could use a program like plot (which I totally didn't write just for this purpose).

$ curl -sS "$api" \
  | jq -Mr '.hourly.data[0:80][] | .temperature' \
  | plot -sascii
      73.79 |                                          .-,
      72.83 |                                          | |
      71.86 |                                         .' `,
      70.90 |                                         |   |
      69.94 |                                        .'   |
      68.97 |                                        |    |
      68.01 +,                                       |    `,
      67.05 ||                                       |     |
      66.08 ||                  .,                  .'     |
      65.12 |`,                .'`,                 |      |
      64.16 | |                |  |                 |      `,
      63.19 | |               .'  `,               .'       |
      62.23 | `,             .'    |               |        `-
      61.26 |  |             |     `,              |
      60.30 |  `,           .'      |              |
      59.34 |   `,          |       `,            .'
      58.37 |    `,        .'        `-,          |
      57.41 |     `,       |           `,         |
      56.45 |      `-,    .'            `-,      .'
      55.48 |        `,   |               `,     |
      54.52 |         `, .'                `,    |
      53.56 |          |.'                  `,  .'
      52.59 |          `'                    `, |
      51.63 |                                 `-'

Finally, here is a script with all of the above bundled into a easy to use interface. It has a few other features, like caching, 3 weather APIs, and x-axis labels on the plot, but if you can live without them, the above is all you need.

Of course, you could use this curl + jq combo and handle just about any http API, without even leaving your shell.


posts · about · hello, japan · rainbloom · home