# 根据一个包含目标 ID 的列表，从一个总的数据 JSON 文件中筛选出匹配的条目，并将筛选结果保存为一个新的 JSON 文件。

import json

# === 配置你的路径 ===
id_json_path =     # 包含id的json
data_json_path =           # 总数据json
output_json_path =  # 输出文件名

def load_json(path):
    with open(path, 'r', encoding='utf-8') as f:
        return json.load(f)

def save_json(data, path):
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

def filter_data_by_ids(all_data, ids):
    id_set = set(ids)
    return [item for item in all_data if item.get("id") in id_set]

if __name__ == "__main__":
    ids = load_json(id_json_path)
    all_data = load_json(data_json_path)
    filtered = filter_data_by_ids(all_data, ids)
    save_json(filtered, output_json_path)
    print(f"共提取 {len(filtered)} 条数据，已保存到 {output_json_path}")
