Home > OS >  I need to compare 2 JSON files in PYTHON with transferring the result to a separate file
I need to compare 2 JSON files in PYTHON with transferring the result to a separate file

Time:01-16

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": "\u0421\u0442\u0440\u0438\u0436\u043a\u0430", "cost": 1500, "cost_per_unit": 1500, "first_cost": 1500, "amount": 1}], "goods_transactions": [], "staff": {"id": 1819441, "name": "\u041c\u0430\u0441\u0442\u0435\u0440"}, "client": {"id": 130345867, "name": "\u041a\u043b\u0438\u0435\u043d\u0442", "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": "\u0421\u0442\u0440\u0438\u0436\u043a\u0430", "cost": 1500, "cost_per_unit": 1500, "first_cost": 1500, "amount": 1}], "goods_transactions": [], "staff": {"id": 1819441, "name": "\u041c\u0430\u0441\u0442\u0435\u0440"}, "client": {"id": 130345867, "name": "\u041a\u043b\u0438\u0435\u043d\u0442", "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 :)

CodePudding user response:

To catch what has changed between json1_dict and json2_dict, you can use the following one line, making good use of "dictionary comprehension":

changed_items = {k: [json1_dict[k], json2_dict[k]] for k in json1_dict if k in json2_dict and json1_dict[k] != json2_dict[k]}

Every key of changed_items will contain two values, first of json1_dict and second of json2_dict. If the changed_items you are interested in must be the keys in diff_list, then you need instead to change a little the condition within the expression:

changed_items = {k: [json1_dict[k], json2_dict[k]] for k in json1_dict if k in json2_dict and k in diff_list and json1_dict[k] != json2_dict[k]}

all you need afterwards is to print(changed_items)

CodePudding user response:

import json

# Load JSON files
with open('json_old.json') as json_file1:
    json1_dict = json.load(json_file1)

with open('json_new.json') as json_file2:
    json2_dict = json.load(json_file2)

diff_list = ["services", "staff", "datetime"]

# Compare the parts specified in diff_list and print the differences
result = {}
for key in diff_list:
    if json1_dict['data'][key] != json2_dict['data'][key]:
        result[key] = {
            'old_value': json1_dict['data'][key],
            'new_value': json2_dict['data'][key]
        }

print(result)

# Write the differences to a result.json file
with open('result.json', 'w') as outfile:
    json.dump(result, outfile)

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.

  • Related