🛠️ToolsShed

JSON-Unterschied

Vergleichen Sie zwei JSON-Objekte und heben Sie hinzugefügte, entfernte und geänderte Schlüssel hervor.

Häufig gestellte Fragen

Code-Implementierung

# pip install deepdiff
from deepdiff import DeepDiff
import json

a = {"name": "Alice", "age": 30, "address": {"city": "Paris", "zip": "75001"}}
b = {"name": "Alice", "age": 31, "address": {"city": "Lyon",  "zip": "75001"}, "email": "alice@example.com"}

diff = DeepDiff(a, b, verbose_level=2)
print(diff)
# {
#   'values_changed': {
#     "root['age']": {'new_value': 31, 'old_value': 30},
#     "root['address']['city']": {'new_value': 'Lyon', 'old_value': 'Paris'}
#   },
#   'dictionary_item_added': ["root['email']"]
# }

# Ignore array order (useful when arrays are used as sets)
a2 = {"tags": ["python", "json", "tools"]}
b2 = {"tags": ["tools", "json", "python"]}
diff2 = DeepDiff(a2, b2, ignore_order=True)
print(diff2)  # {} — no differences when order is ignored

# Pretty-print all changes
for change_type, changes in diff.items():
    print(f"\n{change_type}:")
    if isinstance(changes, dict):
        for path, detail in changes.items():
            print(f"  {path}: {detail}")
    else:
        for path in changes:
            print(f"  {path}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.