How to compare two JSON files: structural diffs that ignore key order and whitespace

Compare JSON by structure: objects by key, arrays by position, numbers by value. Whitespace and key order should never count as changes, and every difference should come with its path.

Comparing JSON as plain text works badly. Reformat a file, let a serializer sort its keys, or move one property, and a line diff lights up the whole document even though no value changed. A structural diff reads both documents first and compares the values, so it reports what changed and where, with a path like $.items[3].price for each difference.

By TechCompare · Updated

Topic
Compare JSON files
json-diff
Category
Comparing
JSON knowledge base

How this is calculated

JSON objects are unordered by definition, so {"a":1,"b":2} and {"b":2,"a":1} hold the same data. Arrays are ordered, so [1,2] and [2,1] are different, unless the array is really a set, like a list of tags. A good diff lets you pick. Arrays need care in another way too. Insert one item at the start of a list and a naive comparison sees every later index as changed. Lining the arrays up with a longest common subsequence fixes that, so the insert shows as one addition. Numbers need a rule as well. 1.0 and 1 are the same value written two ways, while 9007199254740993 and 9007199254740992 are different values that JavaScript happens to read as the same double. A diff that runs JSON.parse first can call those equal, which is exactly the wrong answer for IDs. Repeated keys are the last trap. When an object has the same key twice, parsers disagree on which copy wins, so an honest diff refuses to guess.

Verdict

Use a structural diff to review API changes, config edits and fixtures. Keep array order on for lists where position matters, like steps or rankings, and turn it off for sets like tags or permissions. Treat a repeated key as a bug to fix before comparing. When a diff says two documents match, it should mean every key and every exact value matches, not just that the formatted text looked similar.

More JSON scenarios

Frequently asked questions

Why does my text diff show changes when the JSON is the same?
Text diffs compare lines. A different indent, keys in another order or a value moved to a new line all count as changes, even when the data is identical. A structural diff parses both sides and compares values, so only real changes show up.
Should a JSON diff ignore array order?
Only when the array is a set. For tags, roles or feature flags, order usually doesn't matter, so matching equal items anywhere avoids noise. For steps, rankings or time series, order is part of the data, and moving an item is a real change.
Are 1.0 and 1 different in JSON?
They're written differently but hold the same number, so a structural comparison treats them as equal. The TechCompare compare mode counts these pairs and says so in the result, so you know the text differs even though the value doesn't.