问题: 有两个json体,可是,需要验证json之间的内容有什么差异,然后,就想写一个json,尝试 桌面创建一个python文件,和创建的python文件项目目录下创建 json1.txt json2.txt 分别存放两个json体
"""
@File : checkJson.py
@DataTime : 2022-05-04 08:15
@Author : ChairDu
@Email : chair7@163.com
@Description : 验证json字符串差异
"""
"""
json校验,生成新文件存放当前文件目录下
需要新建文件
json1.txt
json2.txt
分别存放两个json体
"""
import os
import sys
import json
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
txt_path = sys.path[0]
json1 = open(r"{0}\json1.txt".format(txt_path),encoding='utf-8').read()
json2 = open(r"{0}\json2.txt".format(txt_path),encoding='utf-8').read()
finalJson = []
global x
x = 0
def checkJson(json1,json2):
jsons = {**json1,**json2}
json_ture, json_false,json1_null,json2_null = [],[],[],[]
for json_keys in jsons:
global x
yyyyy = "JOSN体整体比对:{0}".format("")
if json1.get(json_keys) == json2.get(json_keys):
content = json_keys
json_ture.append(content)
elif bool(json1.get(json_keys)) == False:
content = json_keys
json1_null.append(content)
elif bool(json2.get(json_keys)) == False:
content = json_keys
json2_null.append(content)
else:
if type(json1.get(json_keys)) == type({}):
x = json_keys
checkJson2(json1.get(json_keys),json2.get(json_keys))
if type(json1.get(json_keys)) == type([]):
x = json_keys
checkJsonList(json1.get(json_keys),json2.get(json_keys))
content = {json_keys: {"json1":json1.get(json_keys),"json2":json2.get(json_keys)}}
content = json_keys
json_false.append(content)
_jsons = {}
if bool(json_ture) == True:
__jsons = {"比对一致字段": json_ture}
_jsons.update(__jsons)
if bool(json1_null) == True:
__jsons = {"json1为空字段":json1_null}
_jsons.update(__jsons)
if bool(json2_null) == True:
__jsons = {"json2为空字段": json2_null}
_jsons.update(__jsons)
if bool(json_false) == True:
__jsons = {"比对不一致字段": json_false}
_jsons.update(__jsons)
_jsons = {yyyyy:_jsons}
finalJson.append(_jsons)
def checkJson2(json1,json2):
"""验证json内的json体"""
jsons = {**json1, **json2}
json_false, json1_null, json2_null = [], [], []
for json_keys in jsons:
global x
yyyyy = "JOSN详细比对:{0}".format(format(x))
if json1.get(json_keys) == json2.get(json_keys):
pass
elif bool(json1.get(json_keys)) == False:
content = {json_keys: json2.get(json_keys)}
json1_null.append(content)
elif bool(json2.get(json_keys)) == False:
content = {json_keys: json1.get(json_keys)}
json2_null.append(content)
else:
if type(json1.get(json_keys)) == type({}):
x = json_keys
checkJson2(json1.get(json_keys), json2.get(json_keys))
elif type(json1.get(json_keys)) == type([]):
x = json_keys
checkJsonList(json1.get(json_keys), json2.get(json_keys))
else:
content = {json_keys: {"json1": json1.get(json_keys), "json2": json2.get(json_keys)}}
json_false.append(content)
_jsons = {}
if bool(json1_null) == True:
__jsons = {"json1为空字段": json1_null}
_jsons.update(__jsons)
if bool(json2_null) == True:
__jsons = {"json2为空内容": json2_null}
_jsons.update(__jsons)
if bool(json_false) == True:
__jsons = {"不一致内容": json_false}
_jsons.update(__jsons)
if bool(_jsons) == True:
_jsons = {yyyyy:_jsons}
finalJson.append(_jsons)
def checkJsonList(list1,list2):
"""验证json内的list"""
json_false = []
global x
yyyyy = "JOSN详细比对:{0}".format(format(x))
len_list = len(list1) if len(list1) < len(list2) else len(list2)
for i in range(0, len_list):
print()
if len(list1) != len(list2):
content = {"列表长度不一致": "list1:{}\tlist2:{}".format(len(list1),len(list2))}
json_false.append(content)
elif list1[i] == list2[i]:
pass
elif type(list1[i]) == type({}) and type(list2[i]) == type({}):
x = "list"
checkJson2(list1[i], list2[i])
elif type(list1[i]) == type([]) and type(list2[i]) == type([]):
x = "list"
checkJsonList(list1[i], list2[i])
else:
json1_list = "json1_list_{0}".format(i)
json2_list = "json2_list_{0}".format(i)
content = {json1_list: list1[i],json2_list:list2[i]}
json_false.append(content)
_jsons = {}
if bool(json_false) == True:
_jsons = {"list不一致内容": json_false}
if bool(_jsons) == True:
_jsons = {yyyyy: _jsons}
finalJson.append(_jsons)
if __name__ == '__main__':
try:
json1,json2 = json.loads(json1),json.loads(json2)
fil_path = "{0}\checkjson.txt".format(txt_path)
fil = open(fil_path, mode='w+',encoding='utf-8')
checejson = checkJson(json1,json2)
finalJsons = {"finalJson":finalJson}
finalJsons = str(finalJsons).replace("'","\"")
print("校验成功,生成文件:checkjson.txt\t完整路径:{0}".format(fil_path))
fil.write(finalJsons)
fil.close
except:
prin("请检查json格式")
粘贴json分别放到json1.txt json2.txt 文件 cmd页面运行程序 生成了一个json文件,生成的json文件后续需要手动调整格式
|