Filter for dates before today with jq

04 Oct 2021 in TIL

tl;dr: jq '[.[] | select(.date_field > (now | strftime("%Y-%m-%d")))]'

I needed a way to dynamically fetch which versions of Node.js are currently supported. This led me to discover the endoflife.date API which returns a list of Node.js versions.

Unfortunately, this API is a static file that does not support filtering, and it was returning data with an eol date that has already passed.

Here's a snippet of the data:

json
[
{
"cycle": "12",
"release": "2019-04-23",
"lts": true,
"support": "2020-10-20",
"eol": "2022-04-30",
"latest": "12.22.6"
},
{
"cycle": "10",
"release": "2018-04-24",
"lts": true,
"support": "2020-05-19",
"eol": "2021-04-30",
"latest": "10.24.1"
}
]

Given this data, I need to remove cycle: 10 as the eol date has already passed.

You can do this using the select function to filter to matching entries, and the strftime function to get the current date in the correct format.

jq has built in support for ISO8601 format dates, so 2021-10-02 > 2021-10-02. This allows us to build the following expression:

bash
jq '[.[] | select(.eol > (now | strftime("%Y-%m-%d")))]'

Putting it all together we can fetch the active versions of Node.js in a single line:

bash
curl https://endoflife.date/api/nodejs.json | jq '[.[] | select(.eol > (now | strftime("%Y-%m-%d")))]'