Skip to main content

Readable JSON comparison with colorized terminal output.

Project description

philiprehberger-json-diff

Tests PyPI version Last updated

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 project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_json_diff-0.2.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_json_diff-0.2.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_json_diff-0.2.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_json_diff-0.2.1.tar.gz
Algorithm Hash digest
SHA256 8574cc7ab6e31ce16c1c94616eb91be230315ac76e09bba5c54bbb191d79de18
MD5 3839935073fb6645aebda12335b94046
BLAKE2b-256 c85763f565273f41fc673b83b5386ae2d32811098d5a360b4adeaaa526f855bf

See more details on using hashes here.

File details

Details for the file philiprehberger_json_diff-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_json_diff-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07799c25a2c61a20f66c50296e3d8a9e7e9a04a2c56b5fcd99650d99eaeceff7
MD5 c117d57e66322308fcca2c103884e066
BLAKE2b-256 4d90392b7aa2a03ec03b191e4c7aa749324f84827a3dbb09a030b700f46c4b4c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page