jq: The JSON CLI tool

February 7, 2020 | By Brian | Filed in: work.

Thanks to lodash, manipulating JSON from JavaScript is easy. Sometimes we’re working with chunks of JSON and it’s easier to deal with it from the shell. Scenarios like this include digging through log output and that (sometimes painful) time when you are first interacting with a new API.

Working with JSON in the shell is quicker and easier than trying to create a temporary file in your project or IDE, then writing some JavaScript code to open that file, parse its contents and scan for whatever it is you are looking for. Those of you that are skilled with the Node.js shell may disagree, but I’m out to optimize your workflow too!

Hello jq

jq is a CLI (command line interface) for working with JSON. It provides shell friendly features (piping, output redirection, etc) that make it feel as if it’s part of your operating system.

To get started, install jq (on OSX)

brew install jq

Or install it directly from the site

Running the jq command by itself will provide some usage info, but not a ton. You’re going to want to head to the online manual, or the man page (man jq).

Let’s see what jq can do!

jq is normally invoked by “piping” JSON data to it (using the | operator). This can be as simple as an echo command. Here we’ll use the simplest filter ., “which copies jq’s input to its output”.

echo '{"name": "Brian"}' | jq '.'
{
  "name": "Brian"
}

By default, jq will pretty print and colorize its output. Neat!

Because it works with pipes, you can also use commands like curl to get an API response, then pass it through jq like so:

curl https://swapi.co/api/people/1/ | jq '.'
# Run this yourself to see output, it's too long to post here

As you can see this can be very useful when you need a quick sanity check of an API response structure, or if you forget if the key name is eyeColor or eye_color.

jq does more than pretty formatting! It let’s you slice & dice the JSON in many ways. For instance, let’s ask jq to return Luke’s eye_color

curl https://swapi.co/api/people/1/ | jq '.eye_color'
"blue"

Cool!

Thankfully, similar to lodash.get, if we ask for a path that doesn’t exist, it doesn’t die a horrible death

curl https://swapi.co/api/people/ | jq '.does.not.exist'
null

Let’s get more advanced

What if we wanted to get the eye_color of all the people (on the first page of results)? Again, jq provides a syntax similar to lodash.get:

curl https://swapi.co/api/people/ | jq '.results[].eye_color'
"blue"
"yellow"
"red"
"yellow"
"brown"
"blue"
"blue"
"red"
"brown"
"blue-gray"

OK, let’s see if we can make it work a little harder. Let’s get the name of all the people (on the first page of results) that have and eye_color of blue?

curl https://swapi.co/api/people/ | jq '.results[] | select(.eye_color == "blue") .name'
"Luke Skywalker"
"Owen Lars"
"Beru Whitesun lars"

Sweet! This is where the power of jq really shines. There are many more powerful filters, operators and functions I suggest you explore. Hopefully I was able to whet your appetite as this is just the tip of the iceberg of things you can do with jq.

One more

One last quick tip – this is the one I use for making sense of my JSON log messages all the time. If you are in OSX, you have the ability to access the clipboard from the shell using pbcopy and pbpaste. So when I am working with JSON logs, from an application like Splunk, I copy the relevant JSON structure to the clipboard, then run it through jq like this:

pbpaste | jq '.'

The pretty printing alone is super useful. On top of that, you have all the great filtering commands to coerce an intimidating log message into a more approachable piece of data!

Special thanks to https://swapi.co/ for providing a fun API to play with!


One comment on “jq: The JSON CLI tool

  1. […] Previously, I wrote about how useful lodash is when working with JSON in JavaScript. Then we took a look at how to deal with JSON from the command line using the power of jq. […]

Leave a Reply

Your email address will not be published. Required fields are marked *