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, "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.*"})

Filter by paths

from philiprehberger_json_diff import diff_paths

old = {
    "users": {"alice": {"email": "a@old.com"}, "bob": {"email": "b@old.com"}},
    "config": {"timeout": 30, "retries": 3},
}
new = {
    "users": {"alice": {"email": "a@new.com"}, "bob": {"email": "b@new.com"}},
    "config": {"timeout": 60, "retries": 3},
}

# Watch only specific subtrees — wildcards work just like ignore patterns
changes = diff_paths(old, new, ["users.*.email", "config.timeout"])
# Reports: users.alice.email, users.bob.email, config.timeout

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
diff_paths(a, b, paths, *, array_strategy) Compare two dicts/lists and return only changes matching the supplied glob-style path patterns
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:

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.4.0.tar.gz (12.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.4.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_json_diff-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2f19fc3ae49fe560b4920668a4e6076f2471fb1ac9c667a4a9f579991a5d5d70
MD5 2a3cd7a15faad45e4d70cdae79646658
BLAKE2b-256 e9c2bad099a8ea414d9adb182657001100c15c5404c042bdf90b5c73102b25da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_json_diff-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f9e6873a61916b0ff95ac3175418db9470a4d90aa783e828932f27319b2513d
MD5 c8fdc1b31ac5c8d4a070b5f35cf16269
BLAKE2b-256 2a501b55c2f9233e8091558846fc25293b588ab142b47f5599addd410f45da0d

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