FmtDev
Language
Back to blog
March 26, 2026

How to Compare JSON Files: Find Differences Instantly

Learn how to compare JSON files and find differences between two JSON objects. Covers deep comparison, key ordering, formatting issues, and the best tools for JSON diffing.

Why Comparing JSON Files Is Harder Than It Looks

At first glance, comparing two JSON files seems simple. Just look at them side by side and spot the differences. But JSON has properties that make visual comparison unreliable.

Consider these two JSON objects:

{"name":"Alice","age":30,"city":"Paris"}
{"city":"Paris","name":"Alice","age":30}

These are identical in meaning. JSON object keys have no defined order. But if you compare them as text (like using diff in a terminal), they look completely different.

Now consider this:

{"score": 100}
{"score": "100"}

These look almost identical but are fundamentally different. The first has a number, the second has a string. A text comparison might miss this. A proper JSON comparison will catch it.

This is why you need a tool that understands JSON structure, not just text.

Types of JSON Differences

When comparing two JSON objects, differences fall into these categories:

1. Added Keys

A key exists in the second JSON but not in the first:

// Before
{ "name": "Alice" }

// After
{ "name": "Alice", "email": "alice@example.com" }

Difference: email was added.

2. Removed Keys

A key exists in the first JSON but not in the second:

// Before
{ "name": "Alice", "age": 30 }

// After
{ "name": "Alice" }

Difference: age was removed.

3. Changed Values

The same key exists in both but with different values:

// Before
{ "name": "Alice", "city": "Paris" }

// After
{ "name": "Alice", "city": "London" }

Difference: city changed from "Paris" to "London".

4. Type Changes

The same key exists with the same apparent value but different types:

// Before
{ "active": true }

// After
{ "active": "true" }

Difference: active changed from boolean true to string "true". This is a subtle but critical difference that breaks many applications.

5. Nested Differences

Changes deep inside nested objects:

// Before
{
  "user": {
    "address": {
      "city": "Paris",
      "zip": "75001"
    }
  }
}

// After
{
  "user": {
    "address": {
      "city": "Lyon",
      "zip": "69001"
    }
  }
}

A good JSON diff tool shows exactly where in the nested structure the change occurred.

6. Array Differences

Arrays are ordered in JSON, so element position matters:

// Before
{ "tags": ["javascript", "python", "go"] }

// After
{ "tags": ["javascript", "go", "python"] }

These are different because the order changed. Whether this matters depends on your application, but a JSON diff tool should flag it.

Common Comparison Mistakes

Mistake 1: Comparing Formatted vs Minified JSON

// File A (formatted)
{
  "name": "Alice",
  "age": 30
}

// File B (minified)
{"name":"Alice","age":30}

These are identical JSON but look completely different as text. A text diff tool will show every line as changed. A JSON-aware diff tool will show zero differences.

Mistake 2: Comparing With Different Key Orders

// File A
{ "name": "Alice", "age": 30 }

// File B
{ "age": 30, "name": "Alice" }

Identical JSON. Text diff shows differences. JSON diff shows none.

Mistake 3: Ignoring Whitespace Differences

Extra spaces, tabs, or newlines can make text comparisons noisy without representing real data changes.

Mistake 4: Missing Type Differences

{ "count": 0 }
{ "count": null }
{ "count": false }
{ "count": "0" }
{ "count": "" }

All five are completely different values in JSON. A text comparison might not make the distinction clear. A proper JSON comparison must distinguish between numbers, null, booleans, and strings.

Comparing JSON in JavaScript

A simple deep comparison:

function jsonEqual(a, b) {
  return JSON.stringify(sortKeys(a)) === JSON.stringify(sortKeys(b));
}

function sortKeys(obj) {
  if (typeof obj !== 'object' || obj === null) return obj;
  if (Array.isArray(obj)) return obj.map(sortKeys);
  return Object.keys(obj).sort().reduce((acc, key) => {
    acc[key] = sortKeys(obj[key]);
    return acc;
  }, {});
}

This normalizes key order before comparing. It works for equality checks but does not tell you what changed.

For a detailed diff, you need to walk both objects recursively and track additions, removals, and changes at each level.

Comparing JSON in Python

import json

with open('before.json') as f:
    before = json.load(f)

with open('after.json') as f:
    after = json.load(f)

# Simple equality check
if before == after:
    print("Identical")
else:
    print("Different")

Python's == operator performs deep comparison of dictionaries and lists, including type checking. This handles nested objects correctly but does not show you where the differences are.

For detailed diffs, use the deepdiff library:

from deepdiff import DeepDiff

diff = DeepDiff(before, after, ignore_order=True)
print(diff)

When JSON Comparison Matters

API Response Testing

When testing APIs, you compare expected vs actual responses. A JSON diff tool shows exactly which fields changed, making test failures easy to diagnose.

Configuration Management

When deploying configuration changes, comparing the old and new JSON config files prevents accidental deletions or modifications.

Database Migration Verification

After migrating data, comparing JSON exports from the old and new databases confirms that no data was lost or corrupted.

Code Review

When a pull request modifies a JSON configuration file, a proper diff shows the real structural changes instead of noisy whitespace differences.

Debugging

When an API suddenly returns different data, comparing the current response with a known good response pinpoints exactly what changed.

Comparing JSON Files Safely

When comparing JSON files that contain sensitive data (API responses with user data, configuration files with secrets, database exports), be careful about where the comparison happens.

Online JSON diff tools process your data on their servers. Both JSON files are uploaded, compared server-side, and the result is sent back. Your data has been transmitted to and processed by a third party.

If your JSON contains any sensitive information — user records, internal configuration, API keys, business data — use a diff tool that runs entirely in your browser. No data should leave your machine.

The FmtDev JSON Diff Tool processes everything locally. Your data never leaves your browser.

Key Takeaways

  1. JSON key order does not matter — text diff tools give false positives
  2. Type differences (number vs string) are critical but easy to miss visually
  3. Always use a JSON-aware comparison tool, not plain text diff
  4. Normalize formatting before comparing to eliminate noise
  5. Array order matters in JSON — element position changes are real differences
  6. For sensitive data, compare locally — never upload to online tools

Related Articles

Related Tool

Ready to use the Offline JSON Formatter & Beautifier (No Server Logs) tool? All execution is 100% local.

Open Offline JSON Formatter & Beautifier (No Server Logs)