Open Source For You — December 2017

(Steven Felgate) #1

Developers How To


66 | DECEMBER 2017 | OPEN SOURCE FOR YOU | http://www.OpenSourceForU.com

J


SON has become the most prevalent way of consuming
Web APIs. If you try to find the API documentation of
a popular service, chances are that the API will respond
in JSON format. Many mainstream languages even have
JSON parsers built in. But when it comes to shell scripting,
there is no inbuilt JSON parser, and the only hacker way of
processing JSON is with a combination of awk and sed, which
are very painful to use.
There are many JSON parsers apart from jq but, in this
article, we will focus only on this option.

Installation
jq is a single binary program with no dependencies, so
installation is as simple as downloading the binary from
https://stedolan.github.io/jq/, copying the binary in /bin or
/usr/bin and setting permissions. Many Linux distributions
provide jq in the repositories, so installing jq is as easy as
using the following commands:

sudo apt install jq

...or:

sudo pacman -S jq

Installation instructions may vary depending upon the
distribution. Detailed instructions are available at https://
stedolan.github.io/jq/download/.

This article is a tutorial on using jq as a JSON parser and fetching information
about the weather from different cities.

Usage
For this demonstration, version 1.5 of jq was used. All
the code examples are available at https://github.com/
jatindhankhar/jq-tutorial. jq can be used in conjunction
with other tools like cat and curl, by piping, or be used to
directly read from the file, although the former is more
popular in practice. When working with jq, two fantastic
resources can be used. The first one is the documentation at
https://stedolan.github.io/jq/manual/, and the second is the
Online Playground (https://jqplay.org/) where one can play
with jq and even share the snippets.
Throughout this article, we will use different
API endpoints of the MetaWeather API (https://www.
metaweather.com/api). The simplest use of jq is to pretty
format JSON data.
Let’s fetch the list of cities that contain the word ‘new’ in
them, and then use this information to further fetch details of
a particular city, as follows:

curl -sS https://www.metaweather.com/api/location/
search/?query=new

The above command will fetch all cities containing ‘new’
in their name. At this point, the output is not formatted.

[{“title”:”New York”,”location_type”:”City”,”woeid”:24
59115,”latt_long”:”40.71455,-74.007118”},{“title”:”New
Delhi”,”location_type”:”City”,”woeid”:28743736,”latt_long”

Using jq to Consume


JSON in the Shell

Free download pdf