Compare two JSON files with diff, ignoring key order
To compare two JSON files whose keys are in different orders, flatten each one with gronify flatten --sort and hand the two outputs to the system diff. Gronify has no compare command. It writes one json.path = value line per value with object keys sorted at every level, and diff does the comparing, so every changed line carries the full path to its value.
Updated
Flatten both files with --sort, then run diff
The examples use two versions of a small deployment config. new.json has one changed value, one added key, one removed key, and its keys in a different order:
cat old.json{"name":"shop","services":{"api":{"replicas":3,"limits":{"cpu":"500m","memory":"256Mi"},"env":{"LOG_LEVEL":"info","REGION":"eu-west-1"}},"worker":{"replicas":2,"limits":{"cpu":"250m","memory":"256Mi"},"env":{"LOG_LEVEL":"info","REGION":"eu-west-1"}}}}cat new.json{"services":{"worker":{"env":{"REGION":"eu-west-1"},"limits":{"memory":"512Mi","cpu":"250m"},"replicas":2},"api":{"limits":{"memory":"256Mi","cpu":"500m"},"env":{"REGION":"eu-west-1","TIMEOUT_MS":3000,"LOG_LEVEL":"info"},"replicas":3}},"name":"shop"}Plain diff old.json new.json reports the one line as changed and prints both documents in full. In bash or zsh, process substitution gives diff two flattened, sorted streams and needs no temporary files:
diff <(gronify --no-color flatten --sort old.json) <(gronify --no-color flatten --sort new.json)7a8> json.services.api.env.TIMEOUT_MS = 300014d14< json.services.worker.env.LOG_LEVEL = "info"18c18< json.services.worker.limits.memory = "256Mi"---> json.services.worker.limits.memory = "512Mi"> marks lines found only in the second file and < lines found only in the first. TIMEOUT_MS was added under services.api.env, LOG_LEVEL was removed from services.worker.env, and the worker's memory limit went from 256Mi to 512Mi. Every line starts at the root, so it says which service a memory value belongs to. The reordered keys produce no output, because --sort put both documents in the same order before diff read them.
--no-color is a global flag, accepted before or after the command name. Gronify already leaves color off when its output is a pipe, and the flag makes that explicit. The npm command is under install, and the line format has its own guide on flattening JSON from the command line.
Without process substitution: write two files, then diff
<( ) is a bash and zsh feature. POSIX sh and dash stop with a syntax error at the parenthesis. In those shells, write each flattened file with -o and compare the files:
gronify --no-color -o old.gron flatten --sort old.jsonOutput written to old.grongronify --no-color -o new.gron flatten --sort new.jsonOutput written to new.grondiff old.gron new.gron7a8> json.services.api.env.TIMEOUT_MS = 300014d14< json.services.worker.env.LOG_LEVEL = "info"18c18< json.services.worker.limits.memory = "256Mi"---> json.services.worker.limits.memory = "512Mi"The saved files also let you try several diff flags without flattening again. When one side comes from a command, pass - and diff reads that side from standard input:
cat new.json | gronify --no-color flatten --sort | diff old.gron -7a8> json.services.api.env.TIMEOUT_MS = 300014d14< json.services.worker.env.LOG_LEVEL = "info"18c18< json.services.worker.limits.memory = "256Mi"---> json.services.worker.limits.memory = "512Mi"If Gronify fails on one side, it prints the error to stderr and exits with status 1, but diff still runs on whatever reached it. A missing file gives diff empty input. JSON that breaks partway through a top-level array gives it the lines flattened before the error. Either way the diff shows a large block of removed or added lines, so read stderr before trusting it.
How this differs from diff with jq -S
The usual answer to this question is diff <(jq -S . a.json) <(jq -S . b.json). jq with -S pretty-prints the document with object keys sorted, so reordered keys disappear there too. This is jq 1.8.2 on the same pair of files:
diff <(jq -S . old.json) <(jq -S . new.json)7c7,8< "REGION": "eu-west-1"---> "REGION": "eu-west-1",> "TIMEOUT_MS": 300017d17< "LOG_LEVEL": "info",22c22< "memory": "256Mi"---> "memory": "512Mi"The same three changes are reported, with less to locate them by. "memory": "256Mi" appears under both services, and the diff gives only line 22 of the pretty-printed text, so you read upward from there to find the parents. The added key also makes REGION count as changed, because that line gained a trailing comma. Flattened lines have no commas, so one added key is one added line.
The jq form is a good choice for small files, where the parents sit a few lines up, and it needs nothing installed besides jq. It also copes better with an element inserted into an array, as the next section shows.
What --sort does, and why arrays compare by position
--sort orders the keys of every object, at every depth, by UTF-16 code unit, the order of JavaScript's default sort(). Each key's subtree stays directly under it, and values are untouched. Arrays keep their index order, which follows JSON itself: RFC 8259 defines an object as an unordered collection of name/value pairs and an array as an ordered sequence of values.
The cost shows when an element is added at the front of an array. Every later element gets a new index, so its line changes:
cat hosts-old.json{"hosts":["db1","db2","db3"],"region":"eu-west-1"}cat hosts-new.json{"hosts":["db0","db1","db2","db3"],"region":"eu-west-1"}diff <(gronify --no-color flatten --sort hosts-old.json) <(gronify --no-color flatten --sort hosts-new.json)3,5c3,6< json.hosts[0] = "db1"< json.hosts[1] = "db2"< json.hosts[2] = "db3"---> json.hosts[0] = "db0"> json.hosts[1] = "db1"> json.hosts[2] = "db2"> json.hosts[3] = "db3"diff <(jq -S . hosts-old.json) <(jq -S . hosts-new.json)2a3> "db0",One host was added. The flattened comparison reports three changed lines and one new line, and that count grows with the length of the array. Pretty-printed JSON carries no indexes, so the jq form reports the insertion as a single line. Neither form recognizes an array whose elements were only reordered.
For arrays that shift or reorder, use a tool that compares the parsed structure. jd compares JSON and YAML values, and its README documents jd -set a.json b.json to treat arrays as sets, -mset for multisets, and -setkeys to name the keys that identify an object in a set. dyff describes itself as a diff tool for YAML files, and sometimes JSON. Its command is dyff between old.json new.json, and its help text lists --ignore-order-changes to ignore order changes in lists.
Useful diff flags for flattened output
These ran with the diff that ships with macOS. GNU diff 3.10 accepts the same flags and printed the same output.
Unified output with readable names
-u prints unified hunks with three lines of context, and -U1 cuts that to one. With process substitution the header shows names such as /dev/fd/12. --label replaces them:
diff -U1 --label old.json --label new.json <(gronify --no-color flatten --sort old.json) <(gronify --no-color flatten --sort new.json)--- old.json+++ new.json@@ -7,2 +7,3 @@ json.services.api.env.REGION = "eu-west-1"+json.services.api.env.TIMEOUT_MS = 3000 json.services.api.limits = {}@@ -13,3 +14,2 @@ json.services.worker.env = {}-json.services.worker.env.LOG_LEVEL = "info" json.services.worker.env.REGION = "eu-west-1"@@ -17,3 +17,3 @@ json.services.worker.limits.cpu = "250m"-json.services.worker.limits.memory = "256Mi"+json.services.worker.limits.memory = "512Mi" json.services.worker.replicas = 2Only the changed lines
To get a list you can paste into a ticket or pipe into another tool, drop the line-number markers and separators:
diff <(gronify --no-color flatten --sort old.json) <(gronify --no-color flatten --sort new.json) | grep '^[<>]'> json.services.api.env.TIMEOUT_MS = 3000< json.services.worker.env.LOG_LEVEL = "info"< json.services.worker.limits.memory = "256Mi"> json.services.worker.limits.memory = "512Mi"A yes-or-no answer for scripts
diff -q prints one line when the inputs differ and nothing when they match. The exit status is 1 for a difference and 0 for none, so an if can branch on it:
diff -q old.gron new.gron; echo $?Files old.gron and new.gron differ1Make git diff show path-level changes for JSON
Git can run a command on each version of a file and compare the results, which it calls a textconv driver. Git appends the file name to the command, so gronify --no-color flatten --sort works as written. One line in .gitattributes assigns a driver to *.json and one config entry defines it. The driver name flatjson is arbitrary.
echo '*.json diff=flatjson' >> .gitattributesgit config diff.flatjson.textconv "gronify --no-color flatten --sort"git diff -U0 config.jsondiff --git a/config.json b/config.jsonindex af94cd3..2eb3520 100644--- a/config.json+++ b/config.json@@ -7,0 +8 @@ json.services.api.env.REGION = "eu-west-1"+json.services.api.env.TIMEOUT_MS = 3000@@ -14 +14,0 @@ json.services.worker.env = {}-json.services.worker.env.LOG_LEVEL = "info"@@ -18 +18 @@ json.services.worker.limits.cpu = "250m"-json.services.worker.limits.memory = "256Mi"+json.services.worker.limits.memory = "512Mi"That is Git 2.54.0, with config.json changed from the contents of old.json to those of new.json. Git still does the comparing, and Gronify only turns each side into text. git log -p and git show use the driver too. The text after each @@ is the line above the hunk, which is a path. A change that only reorders keys prints nothing in git diff, while git status still lists the file as modified.
.gitattributescan be committed. Thegit configline is stored in each clone's.git/config, so every collaborator runs it once.- The converted output is for reading and cannot be applied as a patch.
git format-patchignores the driver, andgit diff --no-textconvshows the raw text. - If a version of the file is not valid JSON, Gronify exits with an error and Git stops with
fatal: unable to read files to diff.
Comparing large JSON files
How much --sort holds in memory depends on the root of the document. A top-level array, the usual shape of a large export, is written as it is read, one record at a time. Everything inside an object is held until the document ends, or until the enclosing record ends when the root is an array. Gronify keeps up to 64 MiB of line data in memory (GRONIFY_SORT_MEMORY changes the limit), then moves buffers to an anonymous temp file in $TMPDIR, finished ones first, and reads them back at the end. A file that is one very large object therefore needs temp space of roughly the size of its flattened output.
diff then reads two text files that are larger than the minified JSON they came from, because every line repeats its full path. Write the flattened files once with -o and reuse them. When only one part of the document matters, filter both sides first. search --sort filters, then sorts only the lines that match:
diff <(gronify --no-color search --sort old.json services.worker) <(gronify --no-color search --sort new.json services.worker)3d2< json.services.worker.env.LOG_LEVEL = "info"7c6< json.services.worker.limits.memory = "256Mi"---> json.services.worker.limits.memory = "512Mi"Search terms and patterns are covered in the guide on how to grep JSON, and memory use on multi-gigabyte input in the guide on searching large JSON files. Measured figures are under benchmarks.
Use this workflow with a coding agent
To give Claude Code, Cursor, Codex, or another supported agent these sorted comparison and git diff textconv recipes, install the json-compare-sorted-diff skill (see JSON exploration for AI coding agents):
npx skills add 1solomonwakhungu/gronify --skill json-compare-sorted-diff