Guides Pricing

Guide

How to compare two JSON objects

A clear explanation of how JSON diffing works — added, removed, and changed keys across nested objects and arrays — with a diff tool built in.

Try it — JSON Diff
J
JSON Diff
✓ identical

Comparing two JSON objects answers a precise question: what changed between version A and version B? That sounds simple, but doing it correctly means comparing structure and values, not text. A naive character-by-character comparison flags differences that do not matter and misses the ones that do. The tool above performs a structural diff; this guide explains what it is actually computing.

Why a text diff is the wrong tool

JSON is whitespace-insensitive and its object keys are unordered. These two documents are identical as data:

{ "name": "Ada", "role": "admin" }
{
  "role": "admin",
  "name": "Ada"
}

A line-based text diff would report nearly every line as different because of indentation and key order. A structural JSON diff parses both inputs first, then compares the resulting values. Order and formatting drop out, and only real differences remain.

The three kinds of difference

Once both sides are parsed, every difference falls into one of three categories:

  • Added — a key present in the second object but not the first.
  • Removed — a key present in the first object but not the second.
  • Changed — a key present in both, mapping to different values.

For example, comparing this:

{ "version": 1, "debug": true, "region": "us" }

with this:

{ "version": 2, "debug": true, "tier": "pro" }

yields: version changed (12), region removed, tier added, and debug unchanged. A type change counts as a change too — if a value goes from the number 42 to the string "42", that is a difference even though they look similar.

Nested objects and arrays

Real JSON nests. A good diff recurses into nested objects, reporting differences at the exact path where they occur rather than declaring the whole parent object changed. Given:

{ "user": { "name": "Ada", "prefs": { "theme": "dark" } } }

versus:

{ "user": { "name": "Ada", "prefs": { "theme": "light" } } }

the diff points to user.prefs.theme as the single changed value, not the entire user block.

Arrays are different from objects because order is meaningful in an array. Most diffs compare arrays by index: position 0 to position 0, position 1 to position 1, and so on. That means inserting an item at the front of a list can shift every later element and read as many changes, even though you only added one entry. Keep this in mind when diffing ordered lists — sorting both sides first, when order is not semantically important, gives cleaner results.

Practical uses

Structural JSON diffing shows up constantly in development:

  • API responses. Capture a response before and after a code change and diff them to confirm you changed only the fields you intended.
  • Configuration changes. Diff two environment configs to see exactly which settings differ between staging and production.
  • Test fixtures and snapshots. When a snapshot test fails, a structural diff tells you which field broke instead of dumping two large blobs of text.
  • Reviewing generated output. Compare the output of two versions of a serializer or exporter to catch unintended schema drift.

Getting reliable results

Two habits make diffs trustworthy. First, validate both inputs are well-formed JSON before comparing — a structural diff cannot run on broken syntax. Second, decide whether array order matters for your data and normalize accordingly. With those in place, the diff above gives you a precise, path-level list of every added, removed, and changed value between two JSON objects.

Frequently asked questions

No. JSON objects are unordered sets of key/value pairs, so a correct JSON diff matches by key name, not position. Two objects with the same keys and values are equal even if the keys appear in a different order.
A text diff compares characters and whitespace, so reformatting or reordering keys shows up as changes even when the data is identical. A structural JSON diff compares the parsed values, ignoring formatting.
Arrays are ordered, so most diffs compare them position by position. Reordering elements is reported as changes, and a different length shows up as added or removed items.
Yes. The diff reports keys present in one object but not the other as added or removed, and reports keys present in both with different values as changed. Size differences are handled naturally.
A key that exists in both objects but maps to different values is a change. The diff records the old and new value so you can see exactly what differs, including type changes such as a number becoming a string.

Related

Esc
↑↓ navigate open Esc close