Skip to content

jq is insane

1st March 2025

I needed to modify JSON files, and the only language available in that Docker container was Bash. Typically, I would look for a language such as Javascript or Python, as they are easier to write in, but there was no option. I had heard of jq before, but never looked at it properly as the syntax looked unapproachable. It took a while reading the docs and experimenting, but eventually I got my desired output working. It looked similar to this:

bash
jq -r '[countries[].cities[].properties | select(population > 1000000) | .name] | join(",")' data.json

I'm sure it's only scratching the surface with what jq can do. The rough equivalent in Javascript would be:

javascript
import fs from "node:fs";

const data = fs.readFileSync("data.json", "utf8");
const countries = JSON.parse(data);

arr = [];
for (country in countries) {
  for (city in country) {
    if (city.properties.population > 1_000_000) {
      arr.push(city.properties.name);
    }
  }
}
output = arr.join(",");

Bash with jq is my top choice when I need to query and modify JSON files.