12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- """
- @Authors: yangzihao(yangzihao@china-icv.cn)
- @Data: 2024/04/01
- @Last Modified: 2024/04/01
- @Summary: Config json drop duplicate.
- """
- import json
- import os
- def read_json_file(file_path):
- with open(file_path, 'r', encoding='utf-8') as file:
- return json.load(file)
- def compare_leaf_nodes(node1, node2):
- """比较两个叶节点是否相同,不同则返回None"""
- if isinstance(node1, dict) and isinstance(node2, dict):
-
- return {key: compare_leaf_nodes(node1.get(key), node2.get(key)) for key in node1}
- elif node1 != node2:
-
- return None
- else:
-
- return node1
- def compare_json_files(file_paths):
-
- base_json = read_json_file(file_paths[0])
-
- result_json = {}
-
- for file_path in file_paths[1:]:
-
- current_json = read_json_file(file_path)
-
- result_json = compare_leaf_nodes(base_json, current_json)
-
- if None in result_json.values():
- break
- return result_json
- def write_json_file(file_path, data):
- with open(file_path, 'w', encoding='utf-8') as file:
- json.dump(data, file, indent=4, ensure_ascii=False)
- def main():
-
- json_dir = './jsonFiles'
- file_paths = [os.path.join(json_dir, f) for f in os.listdir(json_dir) if f.endswith('.json')]
-
- result_json = compare_json_files(file_paths)
-
- output_file_path = './output1.json'
- write_json_file(output_file_path, result_json)
- print(f'Comparison result saved to {output_file_path}')
- if __name__ == '__main__':
- main()
|