I have run the following Python script to remove duplcates between two json files based on userid:
import json with open("target_user.json", "r", encoding='utf-8') as f1: target = json.load(f1) with open("source_user.json", "r", encoding='utf-8') as f2: source = json.load(f2) target2 = [] for item in target: if item['userid'] not in [x['userid'] for x in source]: target2.append(item) with open('target_user2.json', 'w', encoding='utf-8') as nf: json.dump(target2, nf, indent=4)It generates errors:
TypeError: list indices must be integers or slices, not strfrom this line:
if item['userid'] not in [x['userid'] for x in source]:I have tried the following, but it does not fix:
if dict([item]['userid']) not in [dict[x]['userid'] for x in source]: Here is a sample of my json file:
{"567897068": {"userid": "567897068","status": "UserStatus.OFFLINE","name": "btb appeal","bot": false,"username": "None" },"5994781619": {"userid": "5994781619","status": "UserStatus.OFFLINE","name": "Ahh","bot": false,"username": "hourng999" },"1873973169": {"userid": "1873973169","status": "UserStatus.RECENTLY","name": "Chanthy","bot": false,"username": "Alis_Thy" } }Any help is appreciated.
I have tried the followings advised by John, but it does not:
import json with open("target_user.json", "r", encoding='utf-8') as f1: target = json.load(f1) with open("source_user.json", "r", encoding='utf-8') as f2: source = json.load(f2) target2 = [] for item in target.values(): # print(item) # print scanned results on CMD #if item['userid'] not in [x['userid'] for x in source]: #if item['userid'] not in [x['userid'] for x in source]: if target[item]['userid'] not in [source[x]['userid'] for x in source]: target2.append(item) with open('target_user2.json', 'w', encoding='utf-8') as nf: json.dump(target2, nf, indent=4)