How to Compare Two JSON Files for Differences

Published April 2025 · 4 min read

Method 1: Online (Fastest)

Use our Diff Checker — paste both JSON files and see differences highlighted side by side.

Tip: First format both files with our JSON Formatter so the comparison is line-by-line.

Method 2: Command Line

# Linux/Mac
diff <(jq -S . file1.json) <(jq -S . file2.json)

# Or with colordiff
colordiff <(jq -S . file1.json) <(jq -S . file2.json)

Method 3: Python

import json

with open('file1.json') as f1, open('file2.json') as f2:
    json1 = json.load(f1)
    json2 = json.load(f2)

def diff(a, b, path=""):
    if type(a) != type(b):
        print(f"{path}: type changed {type(a)} → {type(b)}")
    elif isinstance(a, dict):
        for key in set(list(a.keys()) + list(b.keys())):
            if key not in a: print(f"{path}.{key}: added")
            elif key not in b: print(f"{path}.{key}: removed")
            else: diff(a[key], b[key], f"{path}.{key}")
    elif a != b:
        print(f"{path}: {a} → {b}")

diff(json1, json2)

Method 4: VS Code

Right-click first file → "Select for Compare" → Right-click second file → "Compare with Selected"

Related