I am using DeepDiff to compare two nested JSON objects in Python, but I'm encountering an issue when comparing dictionaries with more than two keys. The differences aren't broken down into dictionary_item_added, dictionary_item_removed, and values_changed as I expected. Here's an example:
This is how I want it to behave:
expected = {"name":"Cake","image":{"width":200,"height":{"cm":200,"inch":15}}}
actual = {"name":"Cake","image":{"width":250,"height":{"cm":290,"foot":10}}}
ddiff = DeepDiff(expected, actual)
print(ddiff, '\n\n')
Output:
{'dictionary_item_added': ["root['image']['height']['foot']"], 'dictionary_item_removed': ["root['image']['height']['inch']"], 'values_changed': {"root['image']['width']": {'new_value': 250, 'old_value': 200}, "root['image']['height']['cm']": {'new_value': 290, 'old_value': 200}}}
But when I add one key each to actual and expected, it comes out like this:
expected = {"name":"Cake","image":{"width":200,"height":{"cm":200,"inch":15,"mile":20}}}
actual = {"name":"Cake","image":{"width":250,"height":{"cm":290,"foot":10,"yard":90}}}
ddiff = DeepDiff(expected, actual)
print(ddiff, '\n\n')
Output:
{'values_changed': {"root['image']['width']": {'new_value': 250, 'old_value': 200}, "root['image']['height']": {'new_value': {'cm': 290, 'foot': 10, 'yard': 90}, 'old_value': {'cm': 200, 'inch': 15, 'mile': 20}}}}
Expected Output:
{'dictionary_item_added': ["root['image']['height']['foot']", "root['image']['height']['yard']"], 'dictionary_item_removed': ["root['image']['height']['inch']", "root['image']['height']['mile']"], 'values_changed': {"root['image']['width']": {'new_value': 250, 'old_value': 200}, "root['image']['height']['cm']": {'new_value': 290, 'old_value': 200}}}
How can I achieve my desired result?
As per documentation , Threshold To Diff Deeper is a number between 0 and 1. When comparing dictionaries that have a small intersection of keys, we will report the dictionary as a new_value instead of reporting individual keys changed. If you set it to zero, you get the same results as DeepDiff 7.0.1 and earlier, which means this feature is disabled. The new default is 0.33 which means if less that one third of keys between dictionaries intersect, report it as a new object.
So setting it to 0, will solve your problem. I tried the code
from deepdiff import DeepDiff
expected = {"name":"Cake","image":{"width":200,"height":{"cm":200,"inch":15,"mile":20}}}
actual = {"name":"Cake","image":{"width":250,"height":{"cm":290,"foot":10,"yard":90}}}
ddiff = DeepDiff(expected, actual, threshold_to_diff_deeper=0)
print(ddiff.to_dict())
Output:
{'dictionary_item_added': ["root['image']['height']['foot']", "root['image']['height']['yard']"], 'dictionary_item_removed': ["root['image']['height']['inch']", "root['image']['height']['mile']"], 'values_changed': {"root['image']['width']": {'new_value': 250, 'old_value': 200}, "root['image']['height']['cm']": {'new_value': 290, 'old_value': 200}}}