Comparing two JSON objects sounds like a solved problem. You've got two blobs of data, you want to know what changed. How hard can it be? Harder than it looks, as soon as you stop treating JSON as text and start treating it as structured data.

I run into this constantly: checking whether an API response changed between deploys, writing snapshot tests, reconciling config files, or debugging why a cache invalidation didn't fire. Every one of those needs a semantic comparison, not a string match.

What "comparing JSON" actually means

There are two very different things people mean when they say they want to compare JSON.

  • Equality — are these two objects the same? A yes/no answer.
  • Diff — what is different? Which keys were added, removed, or changed, and to what.

Equality is the easy case and still has traps. A diff is what you usually want in practice, because "they're different" is almost never a useful answer on its own. You want to know the three fields that drifted, not that something, somewhere, moved.

Why not just compare the strings?

Because JSON is a serialization format, and the same logical object can serialize a dozen ways. Consider these two:

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

Byte-for-byte, these differ. Semantically, they're identical. Object key order is not significant in JSON, so a naive stringA === stringB check reports a false difference. Whitespace, indentation, and trailing newlines do the same thing.

The usual fix is to parse, sort keys, and re-stringify before comparing. That handles ordering and whitespace, but it quietly introduces its own problems — which brings us to the gotchas.

The gotchas that actually bite

Number precision and type coercion

JSON doesn't distinguish integers from floats, but your language might. In JavaScript, 1 and 1.0 both parse to the number 1, so they compare equal. But "1" (a string) and 1 (a number) are different, and tools that stringify both sides can accidentally make them look the same. Big integers are worse — anything past Number.MAX_SAFE_INTEGER loses precision when parsed in JS, so two genuinely different IDs can collapse into one value.

Array order

Unlike object keys, array order is significant. [1, 2, 3] and [3, 2, 1] are not equal. But plenty of APIs return arrays where order shouldn't matter — a list of tags, a set of permissions. There's no universal right answer here; you have to decide per-field whether order matters. A tool that treats all arrays as ordered will flag noise; one that ignores order will hide real changes.

null vs missing

This one trips up almost everyone. These are not the same:

{"nickname": null}
{}

One explicitly sets the field to null; the other omits it entirely. Depending on your backend, that distinction might be meaningful (a PATCH that clears a field vs one that leaves it alone). A good diff should surface it rather than paper over it.

Floating-point rounding

If your JSON comes from calculations, 0.1 + 0.2 famously isn't 0.3. Exact comparison of computed floats produces spurious diffs. For those cases you need tolerance-based comparison, which generic diff tools won't do for you.

A short worked example

Say you're comparing an old and new version of a user record.

// before
{
  "id": 42,
  "name": "Ada Lovelace",
  "roles": ["admin", "editor"],
  "active": true,
  "nickname": null
}

// after
{
  "id": 42,
  "name": "Ada L.",
  "roles": ["editor", "admin"],
  "active": true,
  "email": "[email protected]"
}

A semantic diff tells you:

  • name changed from "Ada Lovelace" to "Ada L." — a real change.
  • email was added.
  • nickname was removed.
  • roles — depends on your policy. If order matters, it changed. If it's a set, it's identical.

That last point is the whole game. A dumb comparator buries the genuine name and email changes under a false roles diff. When you're scanning a big object by eye, those false positives are what cost you time.

For quick, one-off comparisons I don't bother writing code at all — I paste both blobs into Devsly's JSON Diff and read the highlighted changes. It parses both sides, so key ordering and whitespace don't generate noise, and it shows added, removed, and changed fields distinctly. Handy when you just want to eyeball what's different between two API responses without spinning up a script.

Doing it in code

When you need this in a test or a pipeline, reach for a library rather than rolling your own. In JavaScript, fast-deep-equal handles boolean equality cleanly, and jest's toEqual gives you a readable diff on failure. If you need a structured diff you can act on, deep-diff or microdiff return a list of change records:

import diff from 'microdiff';

const changes = diff(before, after);
// [
//   { type: 'CHANGE', path: ['name'], ... },
//   { type: 'CREATE', path: ['email'], ... },
//   { type: 'REMOVE', path: ['nickname'], ... }
// ]

The key advantage of a change list over a yes/no answer: you can filter it. Ignore changes under metadata.updatedAt, treat roles as an unordered set, allow numeric tolerance — all by post-processing the list to suit your domain. That's the part no general-purpose tool can decide for you.

Rules of thumb

  • Never compare JSON as raw strings unless you also control the serializer on both ends.
  • Parse first, then compare the data structures.
  • Decide deliberately whether array order matters — per field, not globally.
  • Watch big integers in JavaScript; use a library or string-based IDs if precision counts.
  • Keep null and missing distinct unless you have a specific reason to merge them.
  • For humans eyeballing a change, a visual diff beats a console dump every time. For CI, a structured diff you can filter beats a boolean.

Comparing JSON isn't hard once you accept that "are these equal?" is really a question about your data model, not about bytes. Get the policy right for ordering, nulls, and numbers, and the rest falls into place.

Photo by Florian Olivo on Unsplash