This question is an extended version of this problem.
I have a users.json which has data like this:
{
"0": {
"course": "CSE",
"password": "Amsal",
"username": "1800101253"
},
"1": {
"course": "CSE",
"password": "Amsal2",
"username": "1800101253"
},
"2": {
"course": "CSE",
"password": "Amsal3",
"username": "1800101253"
},
"3": {
"course": "CSE",
"password": "Amsal4",
"username": "1800101253"
},
"4": {
"course": "CSE",
"password": "fjfjgal",
"username": "1800101255"
},
"5": {
"course": "CSE",
"password": "Amsal",
"username": "1804959494"
},
"Total": 6
}
Now I want to delete multiple keys(with their nested dictionaries) which has same username fields. As the previous solution was implemented with pop and as pop changes index of all elements each times whenever it removes a element in the list.
The output should be like this:
{
"0": {
"course": "CSE",
"password": "fjfjgal",
"username": "1800101255"
},
"1": {
"course": "CSE",
"password": "Amsal",
"username": "1804959494"
},
"Total": 2
My test code which works fine for single element removal and sorting the json in the same old order(Special thanks to Andrej Kesely for helping out):
with open("users.json") as jsonFile3:
users = json.load(jsonFile3)
total = users["Total"]
for i in range(total):
if users[f"{i}"]["username"] == f"{username}":
pos=i
if not pos==None:
lst = [users[str(v)] for v in range(users["Total"])]
lst.pop(pos)
users = {str(i): v for i, v in enumerate(lst)}
users["Total"] = len(lst)
with open("users.json",'w') as jsonFile4:
json.dump(users,jsonFile4, indent=4, sort_keys=True)