JSON Diff
Compare two JSON objects and highlight added, removed, and changed keys.
Frequently Asked Questions
Code Implementation
# 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.