Readable JSON comparison with colorized terminal output.
Project description
philiprehberger-json-diff
Readable JSON comparison with colorized terminal output.
Installation
pip install philiprehberger-json-diff
Usage
from philiprehberger_json_diff import diff, format_diff, diff_summary
old = {"name": "Alice", "age": 30, "city": "NYC"}
new = {"name": "Alice", "age": 31, "country": "US"}
changes = diff(old, new)
# Pretty-print with colors
print(format_diff(changes))
# Get a summary
summary = diff_summary(changes)
# {"added": 1, "removed": 1, "modified": 1, "unchanged": 0}
Nested Comparison
from philiprehberger_json_diff import diff
old = {"user": {"name": "Alice", "settings": {"theme": "dark"}}}
new = {"user": {"name": "Alice", "settings": {"theme": "light"}}}
changes = diff(old, new)
# Reports: modified user.settings.theme: "dark" -> "light"
Ignore Paths
from philiprehberger_json_diff import diff
changes = diff(old, new, ignore={"user.settings.theme"})
Wildcard Ignore Patterns
from philiprehberger_json_diff import diff
old = {"a": {"metadata": {"ts": 1}}, "b": {"metadata": {"ts": 2}}}
new = {"a": {"metadata": {"ts": 99}}, "b": {"metadata": {"ts": 99}}}
# Ignore metadata fields at any depth
changes = diff(old, new, ignore={"*.metadata.*"})
Structural Diff Mode
from philiprehberger_json_diff import diff
old = {"a": 1, "b": "hello", "c": 3}
new = {"a": "one", "b": "world", "d": 4}
result = diff(old, new, mode="structural")
result.key_additions # [Change(path='d', ...)]
result.key_removals # [Change(path='c', ...)]
result.value_changes # [Change(path='b', ...)]
result.type_changes # [Change(path='a', ...)]
Apply Patch
from philiprehberger_json_diff import diff, apply_patch
old = {"name": "Alice", "age": 30}
new = {"name": "Bob", "age": 31}
changes = diff(old, new)
result = apply_patch(old, changes)
# result == {"name": "Bob", "age": 31}
JSON Patch (RFC 6902)
from philiprehberger_json_diff import diff, to_json_patch
old = {"name": "Alice", "age": 30}
new = {"name": "Bob", "age": 31, "active": True}
patch = to_json_patch(diff(old, new))
# [
# {"op": "replace", "path": "/age", "value": 31},
# {"op": "replace", "path": "/name", "value": "Bob"},
# {"op": "add", "path": "/active", "value": True},
# ]
Array Diff Strategies
from philiprehberger_json_diff import diff, ArrayStrategy
old = {"tags": ["python", "json", "diff"]}
new = {"tags": ["diff", "json", "python"]}
# Default: order-sensitive (reports changes at each index)
changes = diff(old, new)
# Order-insensitive: treats arrays as unordered collections
changes = diff(old, new, array_strategy=ArrayStrategy.ORDER_INSENSITIVE)
# No changes reported — same elements, different order
HTML Output
from philiprehberger_json_diff import diff, format_html
changes = diff(old, new)
html = format_html(changes)
# Returns an HTML <table> with rows classed "added", "removed", or "modified"
API
| Function / Class | Description |
|---|---|
diff(a, b, ignore, mode, array_strategy) |
Compare two dicts/lists, returns list of Change objects or StructuralDiff |
format_diff(changes, color) |
Format changes as readable string with optional ANSI colors |
format_html(changes) |
Format changes as an HTML table for web UIs |
diff_summary(changes) |
Return dict with counts by change type |
to_json_patch(changes) |
Convert changes to RFC 6902 JSON Patch format |
apply_patch(target, changes) |
Apply a diff result as a patch to reconstruct the modified object |
Change |
Dataclass with path, change_type, old_value, new_value |
ChangeType |
Enum: ADDED, REMOVED, MODIFIED, UNCHANGED |
StructuralDiff |
Dataclass with key_additions, key_removals, value_changes, type_changes |
ArrayStrategy |
Enum: ORDER_SENSITIVE, ORDER_INSENSITIVE |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_json_diff-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_json_diff-0.3.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f312cc2d5cef043ff1e4328b94987f077dd4b15b83cd39f1d64110ec1536088
|
|
| MD5 |
d5c231e2d1c973b3150758fa5404c252
|
|
| BLAKE2b-256 |
4a0bb64884b6373df40908249f5e571a5c7bfe13c85c19345fee4325126235b5
|
File details
Details for the file philiprehberger_json_diff-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_json_diff-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41b824ddc1ff8185c8ac75c3303f33a3dc5ae465304a6e2e75f42c32371255f3
|
|
| MD5 |
16af88d51e7f5668e06546b51bec0301
|
|
| BLAKE2b-256 |
022217124a6fc829df2e7922b2392427d1175f491d98763bf52efea52759222f
|