正在加载,请稍候…

JSON Diff: How to Compare Two JSON Objects and Find What Changed

Learn how to compare JSON objects online to find added, removed, and changed keys. Covers common use cases like API response comparison, config drift detection, and debugging.

What Is a JSON Diff?

A JSON diff is a comparison between two JSON objects that identifies every key that was added, removed, or had its value changed. Unlike a text diff (which compares raw lines), a JSON diff understands structure: it matches objects by key, arrays by position (or by a key field), and reports changes at the semantic level rather than the character level.

This makes JSON diff far more useful for developers than piping two JSON files into diff. A text diff would show the entire object as changed if you simply reformatted the indentation. A JSON diff ignores formatting and focuses on the data.

Why Developers Need JSON Diff

Debugging API Changes

You made a change to a backend endpoint and need to confirm the response structure is exactly what the frontend expects. Compare the before and after responses to see what changed.

Detecting Configuration Drift

Two environments (staging and production) should have identical configs, but something went wrong. A JSON diff instantly reveals which keys diverged and by how much.

Code Review

A pull request changes a JSON schema, a fixture file, or a package-lock.json. A semantic diff shows meaningful changes without noise from reformatting.

Regression Testing

Store expected API responses as JSON fixtures. After a code change, diff the new response against the fixture to catch unintended changes.

Auditing State Changes

In Redux or similar state management patterns, log state snapshots and diff them to understand exactly what actions changed.

Types of Differences

A JSON diff typically reports three types of changes:

Added Keys

A key exists in the second (new) object but not the first (old):

// Old
{ "name": "Alice", "role": "admin" }

// New
{ "name": "Alice", "role": "admin", "email": "alice@example.com" }

// Diff: + "email": "alice@example.com"

Removed Keys

A key exists in the first (old) object but not the second (new):

// Old
{ "name": "Alice", "legacy_id": 12345 }

// New
{ "name": "Alice" }

// Diff: - "legacy_id": 12345

Changed Values

A key exists in both objects but the value is different:

// Old
{ "status": "pending", "retries": 0 }

// New
{ "status": "complete", "retries": 3 }

// Diff: ~ "status": "pending" → "complete"
//       ~ "retries": 0 → 3

Nested Object Diffing

A good JSON diff recursively compares nested objects:

// Old
{
  "user": {
    "name": "Alice",
    "address": { "city": "Paris", "country": "FR" }
  }
}

// New
{
  "user": {
    "name": "Alice",
    "address": { "city": "Lyon", "country": "FR" }
  }
}

// Diff: ~ user.address.city: "Paris" → "Lyon"

Reporting the full path (user.address.city) rather than just the key name (city) is essential for large, deeply nested objects.

Array Diffing Challenges

Arrays are harder to diff than objects because elements don't have inherent identifiers. Two strategies:

Index-Based Diffing

Compare elements at the same array index. Simple, but sensitive to reordering: if an element is inserted at the start, everything after it appears as "changed."

Key-Based Diffing

If array elements are objects with a known identifier field (like id or slug), match elements by that key before comparing. This correctly handles reordering, insertion, and deletion.

Practical Examples

Comparing API Responses

// Fetch before and after your change
const before = await fetch('/api/users/1').then(r => r.json());
// ... make your change ...
const after = await fetch('/api/users/1').then(r => r.json());

// Paste both into the JSON Diff tool to see what changed

Comparing package.json Files

When upgrading dependencies, diffing the old and new package.json confirms which packages changed versions and which new dependencies were added.

Comparing Config Files

Environments often store config as JSON. Diffing config.staging.json against config.production.json surfaces any values that should match but don't.

JSON Diff vs Text Diff

Feature JSON Diff Text Diff
Understands structure
Ignores formatting
Handles key reordering
Works without JSON knowledge
Works on non-JSON content
Shows path to changed key

For JSON content, always use a semantic JSON diff. Reserve text diff for non-structured content.

How to Read a Diff Output

Standard diff notation uses symbols:

  • + (green): Added in new version
  • - (red): Removed from old version
  • ~ or no symbol (yellow): Value changed
  • No change: Key exists in both with identical values (often hidden to reduce noise)

When comparing large JSON objects, use the "hide unchanged keys" option to focus only on what's different.

→ Use the JSON Diff tool to paste two JSON objects side-by-side and get an instant, structured comparison highlighting every change.