Comparing JSON: what counts as a difference?

short answer

Two JSON documents are equal when they hold the same data, not when their text matches. Key order, whitespace and how a number is written (1 vs 1.0) do not matter. Everything else does: types, null vs a missing key, string case, and the order of array items.

Two traps catch most people: numbers above 253 that JavaScript silently rounds, and duplicate keys that parsers handle differently.

The rules at a glance

Each pair below was run through the compare tool when this page was built. The result column is its output.

LeftRightResultWhy
{"a":1,"b":2} {"b":2,"a":1} Equal Objects are unordered
{"price":9.90} {"price":9.9} Equal Same number, written differently
{"n":1} {"n":1.0} Equal Same number
{"id":9007199254740993} {"id":9007199254740992} 1 difference Different numbers (JSON.parse calls them equal)
{"n":1} {"n":"1"} 1 difference Number vs string
{"x":null} {} 1 difference A key set to null still exists
{"s":"Ada"} {"s":"ada"} 1 difference Strings are case-sensitive
[1,2,3] [3,2,1] 4 differences Arrays are ordered
[1,2,3] [3,2,1] Equal Only if you choose to ignore array order

Key order

The JSON standard (RFC 8259) defines an object as an unordered collection of name/value pairs. Serializers often change key order: a Python dictionary, a Go struct and a database driver can each write the same record with the keys in a different sequence. A comparison that cares about key order reports noise, which is why a plain text diff of two API responses is often useless.

Numbers: 1, 1.0 and big integers

JSON has one number type, and the standard does not distinguish 1 from 1.0 or 1e0. They are the same value and should compare equal.

The trap is the other direction. JavaScript stores every number as a 64-bit float, which holds integers exactly only up to 253 (9,007,199,254,740,992). Above that, JSON.parse rounds without warning: JSON.parse("9007199254740993") returns 9007199254740992. Database IDs, Twitter/X post IDs and nanosecond timestamps routinely exceed that limit. Any comparison that parses with JSON.parse first will call two different IDs equal.

The compare tool reads the digits as written and compares exact decimal values, so it catches this, and it flags numbers that a JavaScript program would round. If your own code compares such values, parse them as strings or BigInt. Many APIs send large IDs as strings for this reason.

null vs a missing key

{"middleName": null} says the field exists and is empty. {} says nothing about it. Many APIs treat them differently: in a JSON Merge Patch, sending null deletes the field (see JSON Patch vs Merge Patch). So a correct comparison reports the difference, even when your application happens to treat both the same.

Strings that look like numbers

"42" and 42 are different types and are reported as a change, with the type change shown. This often reveals a real bug: one service sends a numeric ID as a string, another as a number, and a strict equality check in the consumer fails.

Duplicate keys

RFC 8259 says names within an object should be unique, but does not forbid duplicates, and parsers disagree on what to do with {"role":"user","role":"admin"}. JavaScript, Python and Go keep the last value; other parsers reject the document or can be configured to. Security researchers have shown real bugs caused by this mismatch, where a validator and the code that uses the data read different values.

The compare tool follows JSON.parse (last value wins) and shows a warning with the line number, so a duplicate is never silently hidden.

Arrays and order

Arrays are ordered, so [1,2,3] and [3,2,1] differ. Whether that is the answer you want depends on the data: the order of route stops matters, the order of tags usually does not. A good tool lets you choose, and the compare tool offers three ways: detect inserted and removed items, compare by position, or ignore order entirely while still counting duplicates.

Doing it in code

A few safe defaults if you compare JSON in your own code:

  • JavaScript: do not compare JSON.stringify(a) === JSON.stringify(b); it depends on key order. Use a deep-equality function such as Node's util.isDeepStrictEqual or Lodash's isEqual, both of which ignore key order.
  • Python: json.loads(a) == json.loads(b) ignores key order and treats 1 == 1.0 as equal. Python integers do not overflow, so big IDs are safe.
  • Command line: jq -S . a.json sorts keys, which makes a normal text diff of two files far quieter.

Every result shown in this guide was computed when the page was built, by the same code that runs the JSON compare tool, and is checked by automated tests.