Compare JSON Config Files With jq and diff
A noisy JSON diff can hide the one setting that changed. Compare temporary copies with jq and learn what still deserves a closer look.
One person edits a JSON setting. Another editor rewrites the whole file with different spacing and puts all the keys in a new order. By the time you open the diff, it looks like the entire configuration changed. Sorting the keys in temporary copies makes the one change you care about easier to find.
In this lab, you’ll compare a small server configuration with three versions: one with keys rearranged, one with a different port, and one with its routes in a different order. JSON objects don’t give key order a meaning, but arrays keep their order. The method below removes the first kind of noise without hiding the third change.
1. Make four small practice files
Use Bash, jq, and diff in a fresh terminal. Check jq --version first. This lab was tested with jq 1.7 and GNU diffutils 3.10 on Linux. It needs no network after those tools are installed. Use only the synthetic files below, not production configs, which may contain tokens and credentials. The diff would print changed values in full.
umask 077
lab=$(mktemp -d) || exit
cd "$lab" || exit
printf 'Practice folder: %s\n' "$lab"
printf '%s\n' '{"server":{"port":8080,"debug":false},"routes":["home","health"]}' > before.json
printf '%s\n' '{"routes":["home","health"],"server":{"debug":false,"port":8080}}' > same.json
printf '%s\n' '{"routes":["home","health"],"server":{"debug":false,"port":9090}}' > changed.json
printf '%s\n' '{"routes":["health","home"],"server":{"debug":false,"port":8080}}' > array.json
diff -u before.json same.json
The last command shows a removed line and an added line even though the port, debug flag, and route order match. Keep that output in mind for the first normalized comparison. Run the next blocks in the same terminal.
2. Sort keys and write separate copies
Use distinct input and output names. The shell opens the output for writing before jq runs, so redirecting to your input file can erase it.
filter='if length == 1 and (.[0] | type == "object") then .[0] else error("expected exactly one JSON object") end'
normalize() {
jq -sS "$filter" < "$1" > "$2"
}
for name in before same changed array; do
normalize "$name.json" "$name.normalized.json" || exit 1
done
-s collects the parsed inputs so the filter can check that each file contains exactly one top-level object. That catches empty input, arrays as the entire file, and more than one JSON value. -S prints each object’s keys in sorted order, including nested objects. jq uses consistent indentation and leaves array items in their original order.
The loop stops on an error so you don’t compare empty or incomplete output. This is a small-file review technique: -s reads its input into memory, and the shape check is not validation against the app’s configuration schema.
3. Compare the settings that remain
compare() {
local rc=0
diff -u "$1" "$2" || rc=$?
case "$rc" in
0) printf 'No differences in the normalized copies.\n' ;;
1) printf 'Differences shown above.\n' ;;
*) printf 'diff could not compare the files (exit %s).\n' "$rc" >&2
return "$rc" ;;
esac
}
compare before.normalized.json same.normalized.json || exit 1
compare before.normalized.json changed.normalized.json || exit 1
compare before.normalized.json array.normalized.json || exit 1
The first comparison reports no differences. The second shows "port": 8080 replaced by "port": 9090; the third shows "home" and "health" in the opposite order. Whether route order matters to the app is a separate question. Keep the change visible until you know.
GNU diff exits with 0 for no differences, 1 for differences, and 2 on trouble such as a missing file. The wrapper explains those outcomes instead of treating any nonzero result as a broken comparison. A blank diff is useful only once both inputs were parsed and the comparison actually completed.
4. Check that a bad input stops the comparison
Try two valid JSON objects in a single file. This fails the lab’s single-object expectation. The conditional below only prints the expected refusal when jq returns nonzero; it never feeds the rejected output into diff.
printf '%s\n' '{"valid":1} {"extra":2}' > multiple.json
if normalize multiple.json rejected.json; then
printf 'Unexpected: the malformed config shape was accepted.\n' >&2
else
printf 'Input rejected; no comparison was run.\n'
fi
Expect jq’s expected exactly one JSON object error and the refusal message. If real input fails, fix or re-export it at the source before using this workflow. Use unique object keys: parsers can disagree about duplicate names, and this key-sorting exercise does not detect them. It also cannot tell you how your application interprets defaults or different numeric representations.
When finished, use cd ~ to leave the folder. Keep the fixtures or remove only the new directory printed in step 1 after checking its contents. The commands leave the practice inputs unchanged. Close this terminal when finished; its private file-creation setting applies only to this shell.
Comments ()