-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary_changes.py
58 lines (40 loc) · 1.67 KB
/
summary_changes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import json
def get_item(data: dict, key: int, keys: list):
if key == len(keys) - 1:
return data
return get_item(data[keys[key]], key + 1, keys)
def get_summary(json_path_old: str, json_path_new: str):
summary = []
with open(json_path_old, 'r', encoding='utf-8') as f_old:
json_old_data: dict = json.load(f_old)
with open(json_path_new, 'r', encoding='utf-8') as f_new:
json_new_data: dict = json.load(f_new)
differences = compare_json_values(json_old_data, json_new_data)
for dif in differences:
j_keys = dif.split('.')
target_old = get_item(json_old_data, 0, j_keys)
target_new = get_item(json_new_data, 0, j_keys)
value_old = target_old[j_keys[-1]]
value_new = target_new[j_keys[-1]]
summary.append([dif, [value_old, value_new]])
return summary
def compare_json_values(obj1, obj2, path=""):
"""
Рекурсивно сравнивает значения в двух словарях JSON.
"""
differences = []
for key in obj1:
current_path = f"{path}.{key}" if path else key
if key not in obj2:
differences.append(current_path)
elif isinstance(obj1[key], dict) and isinstance(obj2[key], dict):
differences.extend(compare_json_values(obj1[key], obj2[key], current_path))
elif obj1[key] != obj2[key]:
differences.append(current_path)
return differences
if __name__ == '__main__':
old = os.path.join(os.getcwd(), r'data/data_fields/fields_from_form.json')
new = os.path.join(os.getcwd(), r'data/data_fields/fields_from_form2.json')
res = get_summary(old, new)
print(res)