import os import json def load_json(file_path): with open(file_path, 'r', encoding='utf-8') as f: return json.load(f) def get_unique_key(base_key, existing_keys): """在已有 key 中查找唯一 key,例如 key, key_1, key_2...""" if base_key not in existing_keys: return base_key i = 1 while f"{base_key}_{i}" in existing_keys: i += 1 return f"{base_key}_{i}" def merge_all_jsons_in_folder(folder_path='.', output_path="merged_all_unique.json"): merged_data = {} for filename in os.listdir(folder_path): if filename.endswith(".json"): file_path = os.path.join(folder_path, filename) try: data = load_json(file_path) if not isinstance(data, dict): print(f"{filename} 不是一个字典,跳过。") continue for key, value in data.items(): unique_key = get_unique_key(key, merged_data) if unique_key != key: print(f"键 '{key}' 重复,已改为 '{unique_key}' 来合并。") merged_data[unique_key] = value except Exception as e: print(f"加载 {filename} 出错:{e}") with open(output_path, 'w', encoding='utf-8') as f: json.dump(merged_data, f, ensure_ascii=False, indent=2) print(f"\n合并完成,共 {len(merged_data)} 条记录写入 {output_path}") if __name__ == "__main__": merge_all_jsons_in_folder(folder_path='.', output_path="merged_all_unique.json")