brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: one empty filter checks that a JSON file parses

Json
~/tips/tip-json-empty-validate

jq empty config.json

Catch a broken config before the program that reads it does.

One stray comma is enough to stop a service from starting. Before I restart anything after editing a JSON file, a quick parse check is cheap insurance.

jq empty config.json

empty is a filter that produces no output, so a good file gives you silence and exit code 0. A broken one gives a parse error with the line and column, and a non-zero exit code. A trailing comma shows up like this:

$ printf '{"a":1,}' | jq empty
jq: parse error: Expected another key-value pair at line 1, column 8

In a script it fits nicely before a restart:

jq empty config.json && docker compose restart app

The restart only happens if the file parses.

It checks syntax and nothing more. A misspelled key or a number where a string belongs passes happily. An empty file also passes, and so do two JSON values one after the other, which a strict parser would reject. If those matter, test for the shape you expect as well. Files with comments, which some programs allow in their config, will be reported as broken.

Further reading: jq manual

More Json tips echo '{"id":1,"payload":"{\"user\":\"ann\",\"tags\":[\"x\",\"y\"]}"}' | jq -c '.payload | fromjson | .tags' Paths() tells you where in a big JSON document a value lives jq -c --slurpfile u users.json ' jq -cn --stream 'fromstream(1|truncate_stream(inputs))' big.json const text = '{"createdAt":"2026-11-30T09:00:00Z","name":"x"}'; All Json tips (9) All topics

Link