按键比较2个JSON文件,并使用python

时间:2018-04-17 06:53:26

标签: python json

JSON1:

{ “测试”:{ “名称”: “测试”, “桶”:{ “名称”: “ ”},“ 格式 ”:{“ JSON ”:{“ 路径 ”:“”}}, “状态”:{ “ID”: “01658204657296583104”, “模式”:{ “InputSchema”:{ “RecordColumns”:[{ “映射”: “0”, “名称”: “column0”, “SQLTYPE”:” STRING “},{” 映射 “:” 1" , “名称”: “列1”, “SQLTYPE”: “STRING”},{ “映射”: “2”, “名称”: “列2”, “SQLTYPE” : “STRING”},{ “映射”: “3”, “名称”: “栏3”, “SQLTYPE”: “STRING”},{ “映射”: “4”, “名称”: “column4”,” SQLTYPE “:” STRING “},{” 映射 “:” 5" , “名称”: “column5”, “SQLTYPE”: “STRING”},{ “映射”: “6”, “名称”: “column6” “SQLTYPE”: “STRING”},{ “映射”: “7”, “名称”: “time_column7”, “SQLTYPE”: “PERIOD”},{ “映射”: “8”, “名称”:” column8" , “SQLTYPE”: “STRING”},{ “映射”: “9”, “名称”: “column9”, “SQLTYPE”: “STRING”},{ “映射”: “10”, “名称” : “column10”, “SQLTYPE”: “NUMBER”},{ “映射”: “11”, “名称”: “column11”, “SQLTYPE”: “STRING”},{ “映射”: “12”,”名称 “:” column12" , “SQLTYPE”: “STRING”},{ “映射”: “13”, “名称”: “column13”, “SQLTYPE”: “STRING”},{ “映射”: “14” , “名称”: “column14”, “SQLTYPE”: “NUMBER”},{ “映射”: “15”, “名称”: “time_column15”, “SQLTYPE” : “期间”},{ “映射”: “16”, “名称”: “column16”, “SQLTYPE”: “NUMBER”},{ “映射”: “17”, “名称”: “column17”,” SQLTYPE “:” STRING “},{” 映射 “:” 18" , “名称”: “column18”, “SQLTYPE”: “STRING”}], “RecordEncoding”: “UTF-8”, “RecordFormat”:{ “MappingParameters”:{ “JSONMappingParameters”:{ “RecordRowPath”: “$”}}, “RecordFormatType”: “JSON”}}}, “进度”:[637637], “源”:{” ./ allure.json “:{” LOC “:” ./ allure.json”, “ETAG”: “无”, “ID”: “”}}, “事件”:[]}}}

JSON2:

{ “测试”:{ “名称”: “测试”, “桶”:{ “名称”: “ ”},“ 格式 ”:{“ JSON ”:{“ 路径 ”:“”}}, “状态”:{ “ID”: “01658204657296583104”, “模式”:{ “InputSchema”:{ “RecordColumns”:[{ “映射”: “0”, “名称”: “column0”, “SQLTYPE”:” STRING “},{” 映射 “:” 1" , “名称”: “列1”, “SQLTYPE”: “STRING”},{ “映射”: “2”, “名称”: “列2”, “SQLTYPE” : “STRING”},{ “映射”: “3”, “名称”: “栏3”, “SQLTYPE”: “STRING”},{ “映射”: “4”, “名称”: “column4”,” SQLTYPE “:” STRING “},{” 映射 “:” 5" , “名称”: “column5”, “SQLTYPE”: “STRING”},{ “映射”: “6”, “名称”: “column6” “SQLTYPE”: “STRING”},{ “映射”: “7”, “名称”: “time_column7”, “SQLTYPE”: “PERIOD”},{ “映射”: “8”, “名称”:” column8" , “SQLTYPE”: “STRING”},{ “映射”: “9”, “名称”: “column9”, “SQLTYPE”: “STRING”},{ “映射”: “10”, “名称” : “column10”, “SQLTYPE”: “STRING”},{ “映射”: “11”, “名称”: “column11”, “SQLTYPE”: “STRING”},{ “映射”: “12”,”名称 “:” column12" , “SQLTYPE”: “STRING”},{ “映射”: “13”, “名称”: “column13”, “SQLTYPE”: “STRING”},{ “映射”: “14” , “名称”: “column14”, “SQLTYPE”: “NUMBER”},{ “映射”: “15”, “名称”: “time_column15”, “SQLTYPE” : “期间”},{ “映射”: “16”, “名称”: “column16”, “SQLTYPE”: “NUMBER”},{ “映射”: “17”, “名称”: “column17”,” SQLTYPE “:” STRING “},{” 映射 “:” 18" , “名称”: “column18”, “SQLTYPE”: “STRING”}], “RecordEncoding”: “UTF-8”, “RecordFormat”:{ “MappingParameters”:{ “JSONMappingParameters”:{ “RecordRowPath”: “$”}}, “RecordFormatType”: “JSON”}}}, “进度”:[637637], “源”:{” ./ allure.json “:{” LOC “:” ./ allure.json”, “ETAG”: “无”, “ID”: “01658204657296583104”}}, “事件”:[]}}}

任何人都可以帮我解决这个问题吗?我想要一个python脚本,它将比较两个JSON文件并仅返回不同的键值。

1 个答案:

答案 0 :(得分:0)

这个简单的代码使用递归函数来提取所有嵌入式列表/字典中的所有值。然后,它比较通过解析json1json2获得的两个列表,并检测类似列表索引处的所有不同值:

json1 = {"test":{"name":"test","bucket":{"name":"."},"format":{"JSON":{"path":""}},"state":{"id":"01658204657296583104","schema":{"InputSchema":{"RecordColumns":[{"Mapping":"0","Name":"column0","SqlType":"STRING"},{"Mapping":"1","Name":"column1","SqlType":"STRING"},{"Mapping":"2","Name":"column2","SqlType":"STRING"},{"Mapping":"3","Name":"column3","SqlType":"STRING"},{"Mapping":"4","Name":"column4","SqlType":"STRING"},{"Mapping":"5","Name":"column5","SqlType":"STRING"},{"Mapping":"6","Name":"column6","SqlType":"STRING"},{"Mapping":"7","Name":"time_column7","SqlType":"PERIOD"},{"Mapping":"8","Name":"column8","SqlType":"STRING"},{"Mapping":"9","Name":"column9","SqlType":"STRING"},{"Mapping":"10","Name":"column10","SqlType":"NUMBER"},{"Mapping":"11","Name":"column11","SqlType":"STRING"},{"Mapping":"12","Name":"column12","SqlType":"STRING"},{"Mapping":"13","Name":"column13","SqlType":"STRING"},{"Mapping":"14","Name":"column14","SqlType":"NUMBER"},{"Mapping":"15","Name":"time_column15","SqlType":"PERIOD"},{"Mapping":"16","Name":"column16","SqlType":"NUMBER"},{"Mapping":"17","Name":"column17","SqlType":"STRING"},{"Mapping":"18","Name":"column18","SqlType":"STRING"}],"RecordEncoding":"UTF-8","RecordFormat":{"MappingParameters":{"JSONMappingParameters":{"RecordRowPath":"$"}},"RecordFormatType":"JSON"}}},"progress":[637,637],"sources":{"./allure.json":{"loc":"./allure.json","etag":"None","id":""}},"events":[]}}}
json2 = {"test":{"name":"test","bucket":{"name":"."},"format":{"JSON":{"path":""}},"state":{"id":"01658204657296583104","schema":{"InputSchema":{"RecordColumns":[{"Mapping":"0","Name":"column0","SqlType":"STRING"},{"Mapping":"1","Name":"column1","SqlType":"STRING"},{"Mapping":"2","Name":"column2","SqlType":"STRING"},{"Mapping":"3","Name":"column3","SqlType":"STRING"},{"Mapping":"4","Name":"column4","SqlType":"STRING"},{"Mapping":"5","Name":"column5","SqlType":"STRING"},{"Mapping":"6","Name":"column6","SqlType":"STRING"},{"Mapping":"7","Name":"time_column7","SqlType":"PERIOD"},{"Mapping":"8","Name":"column8","SqlType":"STRING"},{"Mapping":"9","Name":"column9","SqlType":"STRING"},{"Mapping":"10","Name":"column10","SqlType":"STRING"},{"Mapping":"11","Name":"column11","SqlType":"STRING"},{"Mapping":"12","Name":"column12","SqlType":"STRING"},{"Mapping":"13","Name":"column13","SqlType":"STRING"},{"Mapping":"14","Name":"column14","SqlType":"NUMBER"},{"Mapping":"15","Name":"time_column15","SqlType":"PERIOD"},{"Mapping":"16","Name":"column16","SqlType":"NUMBER"},{"Mapping":"17","Name":"column17","SqlType":"STRING"},{"Mapping":"18","Name":"column18","SqlType":"STRING"}],"RecordEncoding":"UTF-8","RecordFormat":{"MappingParameters":{"JSONMappingParameters":{"RecordRowPath":"$"}},"RecordFormatType":"JSON"}}},"progress":[637,637],"sources":{"./allure.json":{"loc":"./allure.json","etag":"None","id":"01658204657296583104"}},"events":[]}}}

def get_values(json, lst):
  if isinstance(json, list):
    for item in json: get_values(item, lst)
  elif isinstance(json, dict):
    for item in json.values(): get_values(item, lst)
  else: lst.append(json)

list1 = []; get_values(json1, list1)
list2 = []; get_values(json2, list2)

diff = [(n, x, y) for n,(x,y) in enumerate(zip(list1, list2)) if x != y]
print(diff)

结果:

[(36, 'NUMBER', 'STRING'), (68, '', '01658204657296583104')]