I have 2 JSON-files and I need to compare them.
json_new.json
{"company_id": 111111, "resource": "record", "resource_id": 406155061, "status": "create", "data": {"id": 11111111, "company_id": 111111, "services": [{"id": 22222225, "title": "u0421u0442u0440u0438u0436u043au0430", "cost": 1500, "cost_per_unit": 1500, "first_cost": 1500, "amount": 1}], "goods_transactions": [], "staff": {"id": 1819441, "name": "u041cu0430u0441u0442u0435u0440"}, "client": {"id": 130345867, "name": "u041au043bu0438u0435u043du0442", "phone": "79111111111", "success_visits_count": 2, "fail_visits_count": 0}, "clients_count": 1, "datetime": "2022-01-25T13:00:00+03:00", "create_date": "2022-01-22T00:54:00+03:00", "online": false, "attendance": 2, "confirmed": 1, "seance_length": 3600, "length": 3600, "master_request": 1, "visit_id": 346427049, "created_user_id": 10573443, "deleted": false, "paid_full": 1, "last_change_date": "2022-01-22T00:54:00+03:00", "record_labels": "", "date": "2022-01-22 10:00:00"}}
json_old.json
{"company_id": 111111, "resource": "record", "resource_id": 406155061, "status": "create", "data": {"id": 11111111, "company_id": 111111, "services": [{"id": 9035445, "title": "u0421u0442u0440u0438u0436u043au0430", "cost": 1500, "cost_per_unit": 1500, "first_cost": 1500, "amount": 1}], "goods_transactions": [], "staff": {"id": 1819441, "name": "u041cu0430u0441u0442u0435u0440"}, "client": {"id": 130345867, "name": "u041au043bu0438u0435u043du0442", "phone": "79111111111", "success_visits_count": 2, "fail_visits_count": 0}, "clients_count": 1, "datetime": "2022-01-25T11:00:00+03:00", "create_date": "2022-01-22T00:54:00+03:00", "online": false, "attendance": 0, "confirmed": 1, "seance_length": 3600, "length": 3600, "master_request": 1, "visit_id": 346427049, "created_user_id": 10573443, "deleted": false, "paid_full": 0, "last_change_date": "2022-01-22T00:54:00+03:00", "record_labels": "", "date": "2022-01-22 10:00:00"}}
In these files, you need to compare the individual parts specified in diff_list:
diff_list = ["services", "staff", "datetime"]
Also code should print result in console, copy and transfer result copy to the file called result.json
My code
import data as data
import json
# JSON string
with open('json_old.json') as json_1:
json1_dict = json.load(json_1)
with open('json_new.json') as json_2:
json2_dict = json.load(json_2)
diff_list = ["services", "staff", "datetime"]
result = [
(sorted(json1_dict.items())),
(sorted(json2_dict.items()))
]
print(sorted(json1_dict.items()) == sorted(json2_dict.items()))
with open('result.json', 'w') as f:
json.dump(result, f)
This code is actually works but I need to catch the change of certain parameters specified in diff_list and output the value: what has changed and for what.
Thank you for your support, guys 🙂
2
Answers
To catch what has changed between
json1_dict
andjson2_dict
, you can use the following one line, making good use of "dictionary comprehension":Every key of
changed_items
will contain two values, first ofjson1_dict
and second ofjson2_dict
. If thechanged_items
you are interested in must be the keys indiff_list
, then you need instead to change a little the condition within the expression:all you need afterwards is to
print(changed_items)
This code snippet will compare the JSON files and print the differences in the parts specified in the diff_list variable to the console. It will also write the differences to a file named result.json.