A minimal, zero-dependency Python library for comparing JSON objects with human-readable output.
Existing JSON diff libraries have their drawbacks:
| Library | Issue |
|---|---|
| deepdiff | Powerful but heavy, complex output |
| jsondiff | Unintuitive API, hard to interpret results |
| dictdiffer | Output as technical tuples, not human-friendly |
| difflib | Operates on strings, not structured JSON |
json-diff-lite is designed to be:
- Zero dependencies
- Minimal API
- Human-readable output
- Perfect for logs, debugging, and CI
pip install json-diff-litefrom json_diff_lite import diff
old = {
"name": "Ana",
"age": 30,
"address": {"street": "Rua A", "number": 100}
}
new = {
"name": "Ana Maria",
"age": 31,
"address": {"street": "Rua B", "number": 100}
}
changes = diff(old, new)
for change in changes:
print(change)Output:
~ address.street: 'Rua A' -> 'Rua B'
~ age: 30 -> 31
~ name: 'Ana' -> 'Ana Maria'
+key: value — Added-key: value — Removed~key: old -> new — Modified
By default, lists are compared by index. Use list_key to match items by a field:
old = {"users": [{"id": 1, "name": "Ana"}, {"id": 2, "name": "Bob"}]}
new = {"users": [{"id": 2, "name": "Bob"}, {"id": 1, "name": "Ana Maria"}]}
# Without list_key (compares by index - shows many false changes)
diff(old, new)
# With list_key (matches by id field)
changes = diff(old, new, list_key="id")
for change in changes:
print(change)Output:
~ users[id=1].name: 'Ana' -> 'Ana Maria'
You can also provide multiple keys as fallback:
diff(old, new, list_key=["id", "name", "key"])- Dicts (recursive)
- Lists (index-based or key-based comparison)
- Primitives:
str,int,float,bool,None
Compare JSON files directly from the command line:
json-diff-lite file1.json file2.jsonOptions:
-q, --quiet— Exit with code 1 if differences found, no output--no-color— Disable colored output-v, --version— Show version
Colors are automatically enabled when outputting to a terminal:
- Green for additions (
+) - Red for removals (
-) - Yellow for modifications (
~)
Exit codes:
0— No differences (or version shown)1— Differences found2— Error (file not found, invalid JSON)
Current version (v0.4.0) does not support:
- Custom objects
- Unordered comparison tolerance
These features may be added in future releases.
# Clone the repository
git clone https://github.com/nilerbarcelos/json-diff-lite.git
cd json-diff-lite
# Install dev dependencies
pip install hatch
# Run tests
hatch run test:run- Basic diff for dict/list/primitives
- Human-readable output
- Tests
- Command-line interface
- Quiet mode for CI
- ANSI colors (auto-detected)
--no-colorflag
- Match list items by key field (
list_keyparameter) - Support for multiple fallback keys
MIT