Many developers store data from a program in a JSON file; other programs reference APIs which require working with JSON. Indeed, you will have no trouble finding a use case for JSON, or its Python equivalent, dictionaries.
You may encounter a JSONDecodeError when you are working with JSON data. In this guide, we’re going to talk about the causes of a JSONDecodeError and how to fix this error.
Python JSONDecodeError
A Python JSONDecodeError indicates there is an issue with the way in which your JSON data is formatted. For example, your JSON data may be missing a curly bracket, or have a key that does not have a value, or be missing some other piece of syntax.
To completely fix a JSONDecodeError, you need to go into a JSON file to see what the problem is. If you anticipate multiple problems coming up in the future, you may want to use a try…except block to handle your JSONDecodeError.
Followed by the JSONDecodeError keyword, you should see a short description which describes the cause of the error.
All properly-formatted JSON should look like this:
{ "key": "value" }
“value” can be any valid JSON value, such as a list, a string, or another JSON object.
An Example Scenario
We are building a program that stores a list of JSON objects which represent which computers have been issued to employees at a business. Each JSON object should look like this:
[ { "name": "Employee Name", "equip_id": "000" } ]
We store these JSON objects in a file called equipment.json. The file only contains one entry:
[ { "name": "Laura Harper", "equip_id" "309" } ]
To read this data into our program, we can use the json module:
import json with open("equipment.json") as file: data = json.load(file) print("Equipment data has been successfully retrieved.")
First, we import the json module which we use to read a JSON file. Then, we use an open()
statement to read the contents of our JSON file. We print out a message to the console that tells us the equipment data has been retrieved once our with statement has run.
Let’s run our code and see what happens:
Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python3.8/json/__init__.py", line 293, in load return loads(fp.read(), File "/usr/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting ':' delimiter: line 4 column 16 (char 47
Our code returns a long error. We can see Python describes the cause of our error after the term JSONDecodeError.
The Solution
Our JSONDecodeError is telling us we are missing a colon (:) in our JSON data. This colon should appear on line four at column 16. If we look at this line of data in our equipment.json file, we can see our JSON is invalid:
"equip_id" "309",
Our code is missing a colon. To fix this error, we should add a colon:
"equip_id": "309",
Now that we have fixed the problem with the way in which our data is represented, we can try to run our program again:
Equipment data has been successfully retrieved.
Our code executes successfully.
Alternatively, we could use a try…except handler to handle this issue so that our code will not immediately return an error if we face another formatting issue:
import json try: with open("equipment.json") as file: data = json.load(file) print("Equipment data has been successfully retrieved.") except json.decoder.JSONDecodeError: print("There was a problem accessing the equipment data.")
If there is an error in our JSON data, this program will return:
There was a problem accessing the equipment data.
Otherwise, the program will read the data and then display the following text on the console:
Equipment data has been successfully retrieved.
Conclusion
The Python JSONDecodeError indicates there is an issue with how a JSON object is formatted. To fix this error, you should read the error message and use it to guide you in fixing your JSON data. Alternatively, you can use a try…except block to catch and handle the error.
Are you interested in learning more about Python coding? Read our How to Learn Python guide. You’ll find expert advice on how to learn Python and a list of learning resources to help you build your knowledge.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.