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, "total": 3}
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}
API
| Function / Class | Description |
|---|---|
diff(a, b, ignore, mode) |
Compare two dicts/lists, returns list of Change objects or StructuralDiff |
format_diff(changes, color) |
Format changes as readable string with optional ANSI colors |
diff_summary(changes) |
Return dict with counts by change type |
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 |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.
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.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_json_diff-0.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba4425737e73bd6fe22953b5dbc10cbeeda8067b6cc68995ed27c52f8cd3313b
|
|
| MD5 |
a86fe1ddc7a23b40d0633d7e08eee948
|
|
| BLAKE2b-256 |
e7430cafb893f348f471f6152ad7856b94aabfa90b1db6e0e40df4539de63a15
|
File details
Details for the file philiprehberger_json_diff-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_json_diff-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.6 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 |
0dbf8243567aab0470f9d7d1dc498dc2668b24493618a126b4258367a84bfcb7
|
|
| MD5 |
40bd44c0537ef7ed206763a5cbf40f37
|
|
| BLAKE2b-256 |
d811be9581b28f8bfca76952bbe67d441283995628956c6816167884e0d40b4d
|