import json with open("mbpp_with_token+cc.json","r",encoding="utf-8") as f: data = json.load(f) i=0 for item in data: item["id"]=i i=i+1 print(i) with open('mbpp.json', 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=False, indent=4)