Comparing two JSON objects comes up constantly: diffing an API response against a snapshot, checking that a config migration didn't drop a field, verifying that your serializer produces the same output after a refactor. It looks like a one-liner. It almost never is.

The core problem is that JSON has structure, and structure doesn't care about the text that represents it. Two documents can be byte-for-byte different and semantically identical. They can also look nearly identical and differ in a way that breaks production.

What "comparing JSON" actually means

There are two very different questions hiding behind the phrase:

  • Are these two documents textually equal? A plain string comparison answers this. It's fast and wrong for almost every real use case.
  • Do these two documents represent the same data? This is a structural comparison, and it's what you actually want 95% of the time.

Structural comparison parses both sides into objects/arrays/primitives and walks the trees, ignoring things that don't matter (whitespace, key order) while respecting things that do (array order, value types).

When you reach for it

  • Debugging API changes. The response "looks the same" but something downstream broke. A diff shows you the one field that flipped from a string to a number.
  • Test assertions. Comparing expected vs actual payloads in integration tests, where you don't control whitespace or key ordering.
  • Config and schema drift. Did the staging config gain or lose keys relative to production?
  • Reviewing generated output. Snapshot files, exported settings, serialized state.

The gotchas that bite everyone

1. Key order is not significant

JSON object keys have no defined order. These are equal:

{"name": "Ada", "id": 1}
{"id": 1, "name": "Ada"}

A naive string diff will scream about this. Any sane structural comparison treats them as identical.

2. Array order is significant

The flip side: arrays are ordered. [1, 2, 3] and [3, 2, 1] are different documents. If your data is really a set and order shouldn't matter, you have to sort before comparing — the comparison tool can't guess your intent.

3. Types are easy to miss

This is the classic one. "1" and 1 look the same in a casual glance but are a string and a number. JSON preserves the distinction, so {"age": "30"} and {"age": 30} are not equal. Loose == comparisons in JavaScript will happily coerce these and hide the bug.

4. Floating-point noise

0.1 + 0.2 is 0.30000000000000004. If one side computed a value and the other hardcoded it, exact numeric comparison fails. For calculated fields you often want tolerance-based comparison, which a strict diff won't give you.

5. null vs missing

A key set to null is different from a key that isn't there at all:

{"nickname": null}
{}

Whether these should count as equal depends entirely on your domain. Decide deliberately rather than letting the tool decide for you.

6. Duplicate keys

The JSON spec doesn't forbid duplicate keys, but most parsers keep the last one and silently drop the rest. If you're comparing raw text that contains duplicates, parsing both sides first can mask real differences in the source.

A worked example

Say you're testing an endpoint that returns a user. Your expected fixture:

{
  "id": 42,
  "name": "Grace",
  "roles": ["admin", "editor"],
  "active": true
}

The actual response from the server:

{
  "name": "Grace",
  "active": true,
  "id": "42",
  "roles": ["editor", "admin"]
}

At a glance it passes the eyeball test. Structurally there are two real differences:

  • id is 42 (number) in the fixture but "42" (string) in the response — a type mismatch, probably a serialization bug.
  • roles has the same elements in a different order. Whether that's a bug depends on whether role order is meaningful.

The reordered name/active/id keys are noise — ignore them.

Doing this by hand across a 200-line payload is miserable. A structural diff highlights exactly those two lines and nothing else. When I just need a fast answer and don't want to wire up a library, I paste both sides into Devsly's JSON Diff and read the result in seconds.

Doing it in code

For tests you'll want it programmatic. In JavaScript, a cheap-and-cheerful equality check for data that's already normalized:

function deepEqual(a, b) {
  if (a === b) return true;
  if (typeof a !== typeof b) return false;
  if (a === null || b === null) return a === b;
  if (Array.isArray(a) !== Array.isArray(b)) return false;

  if (typeof a === 'object') {
    const ka = Object.keys(a);
    const kb = Object.keys(b);
    if (ka.length !== kb.length) return false;
    return ka.every(k => k in b && deepEqual(a[k], b[k]));
  }
  return false;
}

This handles key ordering and nested structures, and it respects types because it uses === at the leaves. It does not handle unordered arrays, numeric tolerance, or null-vs-missing — those are policy decisions you layer on top. For serious test suites, reach for a library (Jest's toEqual, Node's assert.deepStrictEqual, or a dedicated diff package) rather than rolling your own, because edge cases like NaN, Date objects, and circular references will eventually find you.

A quick checklist

  • Parse both sides before comparing — never diff raw strings for semantic equality.
  • Decide up front whether array order matters; sort if it doesn't.
  • Use strict type comparison so "1" ≠ 1 surfaces.
  • Use tolerance for computed floats.
  • Pick a rule for null vs absent keys and stick to it.
  • For one-off debugging, a visual diff tool beats scrolling.

Most JSON comparison pain comes from treating a structured problem like a text problem. Once you separate "what the bytes say" from "what the data means," the right tool for each situation becomes obvious — and the bugs stop hiding in plain sight.

Photo by Florian Olivo on Unsplash