Skip to content
Advertisement

Create / Load json files (json.decoder.JSONDecodeError: Extra data:)

Expecting

I am expecting to create/add new record and load json files using python.

Json Result

JavaScript

I m not sure if JSON is well formatted. Maybe should it be any error when creating this file? What is the best and correct thing to do on this case? i expect return and print that values on terminal on this project that’s why i need to understand what is wrong on this code. As you can see i have a input with a list of values. Hope you could me help on that.

Error:

First time it works well, but 2nd time get the error below, seems that json it not created correct.

JavaScript

Any idea where is my error? Hope you help me on this issue.

Code to create json

JavaScript

Code to load json

JavaScript

Advertisement

Answer

Having a JSON file with multiple entities is not valid, so you have to change your JSON file structure to this:

JavaScript

a JSON file contains a single entity that can contain multiple objects. so, the code must change to something like this:

JavaScript

The problem with your code is the save function, you save it in an inappropriate format (by using a switch, you just append a new JSON object to the old one on the file, so it makes it an invalid JSON file), hence when it attempts to load it with json.load in load function, it will raise that exception.

your code must change to something like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement