Back to main.

Calmcode Shorts

jq logojq

Installation

It's a small tool, but jq is great.

If you're on a mac you can install the tool via;

brew install jq

On many linux distributions you should be able to install it via;

apt-get install jq

You can confirm the installation command for your linux distribution.

Usage

You can get the version information from pip shown as json via;

pip list --format json

This json data can be passed to jq.

pip list --format json | jq

The jq app can wrangle it from here using it's domain specific lanuage. For example; the code below will remove the outer [].

pip list --format json | jq ".[]"

We can also add the -c flag to make the data appear as jsonlines.

pip list --format json | jq -c ".[]"

Because the data is now pasted line by line, we can continue to use grep.

pip list --format json | jq -c ".[]" | grep num

From here you can move on to save the data into a file.

pip list --format json | jq -c ".[]" | grep num > subset.jsonl

In general jq is used to read in a json file that is on disk. This can be done via;

cat filename.json | jq

There are a lot of things that you can do with jq. Feel free to explore the docs. One final reminder is that while jq is indeed very powerful, there comes a moment when you'll want to use a programming language instead of a tool from the terminal to parse the data. If you're interested you might enjoy clumper for this use-case.


Back to main.