Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why json has an erroe when deleted a list in replit

Im trying to delete a single index in a list which is inside a json file


the file

{
   "0": [
      "0",
      "1",
      "2",
      "3"
   ]
}

The program

import json
with open("main.json","r") as jsonfile:
  jsonfile = json.load(jsonfile)
key = "0"

index = int(input("which index u want to remove: "))

with open("main.json","r+") as f:
  if key in jsonfile:
    #jsonfile[key].append(str(index))

    del jsonfile[key][index]
    
    json.dump(jsonfile,f,indent=3)

This is how it looks after the programme deletes the index:
the

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please say how to stop this problem. Im using repl.it

>Solution :

Not exactly sure what is going wrong, but I fixed it and made it work:

import json
with open("main.json","r") as jsonfile:
  jsonfile = json.load(jsonfile)
key = "0"

index = int(input("which index u want to remove: "))

with open("main.json","w") as f:
  if key in jsonfile:
    #jsonfile[key].append(str(index))

    del jsonfile[key][index]
    
    json.dump(jsonfile,f,indent=3)

Works on replit.com, I know because thats what I use.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading