正在加载,请稍候…

JSON Diff: How to Compare Two JSON Objects

Compare two JSON objects and highlight differences. Learn how JSON diff tools work and when to use them.

What Is JSON Diffing?

JSON diffing is the process of comparing two JSON documents to identify what has changed between them. This is essential when debugging API responses, tracking configuration changes, reviewing data migrations, or investigating when and how data changed over time.

A good JSON diff tool goes beyond simple text comparison - it understands the JSON structure and shows meaningful semantic differences.

Types of JSON Changes

When comparing two JSON documents, changes fall into four categories:

Added: Keys/values present in the new version but not the old:

// Old:  { "name": "Alice" }
// New:  { "name": "Alice", "age": 30 }
// Diff: + "age": 30

Removed: Keys/values present in the old version but not the new:

// Old:  { "name": "Alice", "status": "pending" }
// New:  { "name": "Alice" }
// Diff: - "status": "pending"

Changed: Values that exist in both but have different values:

// Old:  { "count": 10 }
// New:  { "count": 15 }
// Diff: ~ "count": 10 -> 15

Unchanged: Keys/values identical in both versions (often hidden in diff views).

Array Comparison Challenges

Arrays are more complex to diff than objects because items don't have inherent keys. Consider:

// Old: [1, 2, 3, 4]
// New: [1, 3, 4, 5]

A naive comparison might say positions 1, 2, 3 all changed. A smarter diff recognizes that element 2 was deleted and element 5 was added.

For arrays of objects, the choice of what to use as a key for matching matters:

// Old: [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
// New: [{"id": 2, "name": "Bobby"}, {"id": 3, "name": "Charlie"}]

Using id as the key: Alice(1) removed, Bob(2) renamed to Bobby, Charlie(3) added.

JSON Diff Algorithms

The JSON Merge Patch (RFC 7396) format describes differences compactly:

// Patch to transform old -> new
{
  "removed_field": null,    // null means delete
  "changed_field": "new_value",
  "new_field": "added_value"
  // absent fields are unchanged
}

The JSON Patch (RFC 6902) format describes differences as a sequence of operations:

[
  { "op": "remove", "path": "/removed_field" },
  { "op": "replace", "path": "/changed_field", "value": "new_value" },
  { "op": "add", "path": "/new_field", "value": "added_value" }
]

JSON Patch supports these operations: add, remove, replace, move, copy, test.

Practical Use Cases

API Response Debugging

When an API response changes unexpectedly, diffing two consecutive responses reveals exactly what changed. This is especially valuable when debugging intermittent issues.

Configuration Management

Comparing configuration files between environments (development vs staging vs production) helps identify environment-specific differences.

Database Migration Verification

After a data migration, diffing JSON exports from before and after verifies that the migration transformed data correctly.

Code Review

Reviewing changes to JSON schema files or sample API responses becomes much clearer with a structural diff tool.

Using This Tool

Paste two JSON documents in the side-by-side editor. The tool:

  • Validates both documents as valid JSON
  • Shows structural differences with color coding (green=added, red=removed, yellow=changed)
  • Handles nested objects and arrays
  • Provides both visual diff and a JSON Patch output

-> Try the JSON Diff Tool