You export the same configuration from two environments, run a diff, and it lights up with hundreds of changes. Nothing is actually different. The same keys and values are present, just in another order, and a line-by-line comparison cannot know that.
The reason is simple: a JSON object has no defined key order. {"a": 1, "b": 2} and {"b": 2, "a": 1} hold exactly the same data, but as text they are different. To compare data, you have to compare it as data, or make the text identical first.
The idea: normalise, then compare
There are two honest ways to get a useful answer:
- Sort the keys in both files, then run an ordinary text diff. Sorting turns equal objects into equal text, so what remains in the diff is real.
- Parse both files and compare the parsed values in code, which ignores layout and key order entirely.
The first is best when you want to see what changed. The second is best when you only need a yes or no. Both rest on the same fact about JSON, and both have a catch worth knowing about before you trust the result.
Method 1: sort the keys, then diff
With jq on Linux or macOS, the -S flag sorts keys, and a process substitution lets you diff the results without making temporary files:
diff <(jq -S . a.json) <(jq -S . b.json)
If Python is installed, its built-in JSON tool does the same job:
python -m json.tool --sort-keys a.json > a.sorted.json
python -m json.tool --sort-keys b.json > b.sorted.json
diff a.sorted.json b.sorted.json
In the browser, with nothing to install: paste each file into the JSON formatter, set Keys to Sort A–Z, and copy the result into the diff checker. The sort is applied to nested objects too, not only the top level, so deeply nested differences in order disappear as well. Nothing is uploaded, which matters when the files are exports of real data.
What a sorted diff still reports
Sorting fixes order, and only order. Here are two objects that hold the same values, with the keys in a different order:
{"name":"Ada","tags":["x","y"],"meta":{"b":2,"a":1},"n":1.0,"price":12.50}
{ "meta": { "a": 1, "b": 2 }, "price": 12.5, "name": "Ada", "n": 1, "tags": ["x","y"] }
After sorting both with the formatter and diffing, almost everything matches. Two lines still differ:
"n": 1.0against"n": 1"price": 12.50against"price": 12.5
As numbers these are identical, but as text they are not, because the formatter keeps each number exactly as it was written. That is deliberate. A formatter that silently rewrote 12.50 to 12.5 would also quietly corrupt values such as very large integers. It does mean a text diff can show a difference that a data comparison would not, so decide up front whether 1.0 and 1 count as the same thing for your purpose.
Array order is different
Sorting keys does nothing to arrays, and it should not. In JSON an array is ordered: ["x", "y"] and ["y", "x"] are different values. If you compare the two objects above with only the tags swapped, the comparison reports a real difference, as it should.
Sometimes an array is really a set, such as a list of permissions or tags where order carries no meaning. A key-sorting step will not help there. You need to sort the arrays yourself before comparing, or use a tool that compares arrays as sets.
Method 2: compare as data
When you need only "are these the same?", parse both and compare the values. In Node.js:
import { isDeepStrictEqual } from "node:util";
import { readFileSync } from "node:fs";
const a = JSON.parse(readFileSync("a.json", "utf8"));
const b = JSON.parse(readFileSync("b.json", "utf8"));
console.log(isDeepStrictEqual(a, b));
On the two objects above this prints true: same keys, same values, order and number formatting ignored. With the tags swapped it prints false. This is the right tool for a test that asserts "the API still returns the same data", but it gives you no explanation when the answer is no. For that, go back to a sorted text diff.
Pitfalls worth checking
- Duplicate keys. JSON text can repeat a key, and parsers differ on what they do with it. Most keep the last value. A comparison that parses first will hide the duplicate, so look at the raw text if you suspect one.
- Whitespace and line endings. A file saved on Windows may end its lines with
\r\n. Formatting both files the same way before the diff removes that noise. The diff checker also has an option to ignore whitespace. - Floating point.
0.1 + 0.2style differences from different producers can make two numbers that "should" match differ in the last digit. A comparison with a tolerance is a different job from a diff. - Large integers. Values beyond 2^53 lose precision once parsed into a double, so a data comparison can call two different large numbers equal. If your files contain 64-bit IDs, compare them as text.
- Very large files. Sorting loads the whole document into memory. For files of hundreds of megabytes, a command-line tool is a better fit than a browser tab.
Which method to pick
- You want to see what changed: sort both files, then diff them as text.
- You want a yes or no in a script or a test: parse and deep-compare.
- The files hold secrets or customer data: use something that runs locally, which includes the command-line tools above and the browser tools that do not upload anything.
- Arrays are really sets: sort those arrays first, or compare them separately.
The worst outcome is not a noisy diff but a reassuring one. Before you rely on a "no differences" result, read the section above on what normalisation ignores, and check that it is ignoring only what you intended.
