Back to list

Long jq


jq enables many pretty printing possibilities.

$ echo '[1,2,4,5]' | jq

Practical example: nodejs latest LTS download

I want a script to always download the latest LTS from nodejs.

Nodejs does not provide a pointer to the latest LTS, latest points to the "bleeding edge", 14 at time of writing.

Hopefully nodejs team provides a json file that we will parse which lists all the existing versions in reverse order.

$ curl -sS | jq
"version": "v14.2.0",
"date": "2020-05-05",
"files": [
"npm": "6.14.4",
"v8": "8.1.307.31",
"uv": "1.37.0",
"zlib": "1.2.11",
"openssl": "1.1.1g",
"modules": "83",
"lts": false,
"security": false
---8<--- truncated ---8<---

To be able to retreive the version we can filter with .[].version:

$ curl -sS | jq '.[].version'
---8<--- truncated ---8<---

This is not helping a lot as we have no clue which is the LTS. Hopefully, the lts property is "not false" when the version is an LTS -- each has a name.

Here comes some magic:

$ curl -sS | jq 'map(select(.lts != false)) | .[0].version'

jq will loop through the items of the original array, apply the requested filter (lts attribute not being false), create an array with the results, pass it further (the pipe "|") to take the first entry's version attribute.

The quotes are part of the result. To be able to use it, we need to remove them:

$ version=$(curl -sS | jq -r 'map(select(.lts != false)) | .[0].version')
$ echo $version

This can now be used to download the corresponding nodejs package.

More example probably soon.

jq's documentation is on its website