Skip to content
data2026-07-252 min read

What Is JSON Diff

JSON Diff compares two JSON data structures and identifies differences. It detects additions, deletions, and modifications between two JSON objects.

Comparison Example

Original JSON:

{
  "name": "Alice",
  "age": 28,
  "hobbies": ["reading", "gaming"]
}

Modified JSON:

{
  "name": "Alice",
  "age": 29,
  "hobbies": ["reading", "coding"],
  "email": "alice@example.com"
}

Diff Result:

{
  "age": { "old": 28, "new": 29 },
  "hobbies[1]": { "old": "gaming", "new": "coding" },
  "email": { "added": "alice@example.com" }
}

Why You Need JSON Diff

  1. API response validation: Compare API returns with expected values
  2. Config change detection: Compare configuration file modifications
  3. Data migration verification: Confirm data migration correctness
  4. Code review: Compare JSON output between two versions
  5. Automated testing: Assert data changes in tests
  6. Version control: Track JSON data change history

Diff Result Types

| Change Type | Description | Marker | |-------------|-------------|--------| | Added | Key doesn't exist in original | + or added | | Deleted | Key exists in original but not modified | - or deleted | | Changed | Key exists but values differ | ~ or changed | | Unchanged | Key and value are the same | No marker |

How to Use an Online Tool

Using DevToolkit Pro's JSON Diff Tool:

  1. Enter original JSON on the left
  2. Enter modified JSON on the right
  3. The tool auto-compares and highlights differences
  4. Shows specific additions, deletions, and changes
  5. Copy Diff results supported

JSON Diff in Code

JavaScript

// Deep compare two objects
function deepDiff(oldObj, newObj, path = '') {
  const diffs = {};
  const allKeys = new Set([...Object.keys(oldObj), ...Object.keys(newObj)]);

  for (const key of allKeys) {
    const currentPath = path ? `${path}.${key}` : key;

    if (!(key in oldObj)) {
      diffs[currentPath] = { type: 'added', value: newObj[key] };
    } else if (!(key in newObj)) {
      diffs[currentPath] = { type: 'deleted', value: oldObj[key] };
    } else if (typeof oldObj[key] === 'object' && typeof newObj[key] === 'object') {
      Object.assign(diffs, deepDiff(oldObj[key], newObj[key], currentPath));
    } else if (oldObj[key] !== newObj[key]) {
      diffs[currentPath] = { type: 'changed', old: oldObj[key], new: newObj[key] };
    }
  }

  return diffs;
}

Python

def deep_diff(old, new, path=''):
    diffs = {}
    all_keys = set(list(old.keys()) + list(new.keys()))

    for key in all_keys:
        current_path = f"{path}.{key}" if path else key

        if key not in old:
            diffs[current_path] = {'type': 'added', 'value': new[key]}
        elif key not in new:
            diffs[current_path] = {'type': 'deleted', 'value': old[key]}
        elif isinstance(old[key], dict) and isinstance(new[key], dict):
            diffs.update(deep_diff(old[key], new[key], current_path))
        elif old[key] != new[key]:
            diffs[current_path] = {'type': 'changed', 'old': old[key], 'new': new[key]}

    return diffs

FAQ

Can JSON Diff Handle Large Files?

Depends on implementation. Simple in-memory comparison may be slow for large files (>10MB). Use streaming Diff algorithms or specialized libraries (deep-diff, jsondiffpatch).

Can Diff Results Be Merged?

Some tools support "merge" functionality, applying Diff results to the original JSON. This requires the Diff to contain complete change info (old and new values).

How to Handle Array Order Changes?

Array Diff has two strategies: position-based comparison (simple but may be inaccurate) and content-based comparison (smarter but more complex). Choose based on your use case.


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad