我需要比较PYTHON中的2个JSON文件,并将结果传输到单独的文件

qvsjd97n  于 2023-01-18  发布在  Python
关注(0)|答案(2)|浏览(83)

我有2个JSON文件,我需要比较它们。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"}}

在这些文件中,您需要比较diff_list中指定的各个部分:

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

代码还应在控制台中打印结果,将结果副本复制并传输到名为result.json的文件中
我的代码

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)

这段代码实际上可以工作,但我需要捕捉diff_list中指定的某些参数的变化并输出值:改变了什么,为了什么
谢谢你们的支持,伙计们:)

z18hc3ub

z18hc3ub1#

要了解json1_dictjson2_dict之间发生了什么变化,可以使用下面的一行代码,充分利用“字典理解”:

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]}

changed_items的每个键都包含两个值,第一个是json1_dict,第二个是json2_dict。如果您感兴趣的changed_items必须是diff_list中的键,则需要稍微更改表达式中的条件:

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]}

之后您所需要的就是print(changed_items)

xytpbqjk

xytpbqjk2#

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)

这个代码片段将比较JSON文件,并将diff_list变量中指定的部分的差异打印到控制台,还将差异写入名为result.json的文件。

相关问题