Back to Blog

How to Compare Two JSON Objects: A Developer's Guide

Mar 4, 20265 min read

JSON diffing is comparing two JSON documents to identify what was added, removed, or changed. It is an essential skill for debugging API changes, auditing configuration drift, reviewing data pipeline outputs, and validating test fixtures.

Why JSON Diffing Matters

  • API versioning: Spot breaking changes between v1 and v2 API responses before deploying.
  • Configuration management: Check what changed between two versions of a package.json, tsconfig.json, or infrastructure config.
  • Test fixtures: Compare expected vs. actual JSON in integration tests to pinpoint failed assertions.
  • Data pipelines: Verify that a transformation step produced the correct output by diffing before and after snapshots.

Online JSON Diff Tool

The quickest way to compare two JSON objects is our JSON Diff tool. Paste the two objects side by side, click Compare, and get a colour-coded diff highlighting every addition (green), deletion (red), and modification (yellow). No setup, no login.

Deep Equality in JavaScript

JavaScript's === does reference equality, not value equality. Use a deep-compare approach:

JavaScript — Deep Equality
// Simple deep equality (no diff detail)
function deepEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}

// With microdiff for detailed changes
import { diff } from 'microdiff';

const before = { name: 'Alice', role: 'viewer' };
const after  = { name: 'Alice', role: 'admin', age: 30 };

const changes = diff(before, after);
// [
//   { type: 'CHANGE', path: ['role'], oldValue: 'viewer', value: 'admin' },
//   { type: 'CREATE', path: ['age'], value: 30 }
// ]

Deep Diff in Python

Python — Using deepdiff
from deepdiff import DeepDiff

before = {"name": "Alice", "role": "viewer"}
after  = {"name": "Alice", "role": "admin", "age": 30}

diff = DeepDiff(before, after)
print(diff)
# {'values_changed': {"root['role']": {'new_value': 'admin', 'old_value': 'viewer'}},
#  'dictionary_item_added': ["root['age']"]}

Key Concepts to Know

  • Key order doesn't matter: {"a":1,"b":2} and {"b":2,"a":1} are semantically identical. Good diff tools normalise key order before comparing.
  • Array order does matter: JSON arrays are ordered. Swapping two items counts as two changes.
  • Type coercion: 1 (number) and "1" (string) are different JSON values. A strict diff will flag this mismatch.
  • Whitespace is irrelevant: Diff tools strip whitespace before comparing so indentation differences don't produce false positives.

Format Before Diffing

Always pretty-print both JSON objects before comparing. Minified JSON from different sources can look identical character-for-character but differ in key ordering. Our JSON Diff tool auto-formats both inputs before diffing to eliminate whitespace noise.

Compare JSON Objects Instantly

Paste two JSON objects side by side for an instant colour-coded diff — no login, no install.