之前使用了difflib模块进行对比json数据,这次再次使用deepdiff模块来对比json数据差异,
from compare_json_data import TestDiffJson from deepdiff import DeepDiff data_old=TestDiffJson().old_data() data_new=TestDiffJson().new_data() print(data_old) print(data_new) result = DeepDiff(data_old, data_new) print(result)
输出结果:
{'key1': 'value1', 'key2': 'value2', 'key3': [1], 'object': {'object_key1': 'object_value2', 'object_key2': {'sub_key': 'object_key2_sub_value', 'sub_key3': {'xx': 1}, 'sub_key4': {'xx': 1}}, 'object_key3': [{'list_sub_key1': 'xx'}, {'list_sub_key2': 'xx'}]}, 'x': 1.2} {'key1': 'value1', 'key2': 'value2', 'key3': (1,), 'object': {'object_key1': 'object_value1', 'object_key2': {'sub_key': 'object_key2_sub_value', 'sub_key2': {'xx': 1}, 'sub_key3': 'test'}, 'object_key3': [{'list_sub_key1': 'xx'}, {'list_sub_key2': []}]}, 'x': 1.2} {'type_changes': {"root['key3']": {'old_type': <class 'list'>, 'new_type': <class 'tuple'>, 'old_value': [1], 'new_value': (1,)}, "root['object']['object_key2']['sub_key3']": {'old_type': <class 'dict'>, 'new_type': <class 'str'>, 'old_value': {'xx': 1}, 'new_value': 'test'}, "root['object']['object_key3'][1]['list_sub_key2']": {'old_type': <class 'str'>, 'new_type': <class 'list'>, 'old_value': 'xx', 'new_value': []}}, 'dictionary_item_added': [root['object']['object_key2']['sub_key2']], 'dictionary_item_removed': [root['object']['object_key2']['sub_key4']], 'values_changed': {"root['object']['object_key1']": {'new_value': 'object_value1', 'old_value': 'object_value2'}}}