How to Fix json.decoder.JSONDecodeError: Expecting value

We see json.decoder.JSONDecodeError: Expecting value error when we load the json response/file with load() or loads() and the json response/file cannot be decoded by the json decoder. It can be for various reasons :

  • The JSON response/file is empty.
  • The JSON response/file is not in JSON format.
  • The JSON response/file is not structured properly.
  • The JSON response/file has values that are not understood by the json decoder.

In this article, let’s see the different cases when this error is seen and possible fixes for the json.decoder.JSONDecodeError: Expecting value error.

Also check the similar error, How to Fix JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What causes json.decoder.JSONDecodeError: Expecting value in Python ?

Case 1: The JSON response/file is empty.

Let’s say, the JSON response is empty or the JSON file is empty as shown below.

 

Empty JSON file

 

When we try to load the content from this json file, we see this error.

import json

with open("sample.json") as f:
    json_data=json.loads(f.read())
    print(json_data)

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\jsondecodeerror.py", line 4, in <module>
    json_data=json.loads(f.read())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Let’s try adding some data in the file and see what happens.

Case 2: The JSON response/file is not in Structured format

When the json file or response is malformatted, we see this error.

Let’s say we have a JSON file that is malformatted as shown below.

 

json file containing unstructured data

 

Let’s see what happens when we try to load the contents from this file.

import json

with open("sample.json") as f:
    json_data=json.loads(f.read())
    print(json_data)

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\jsondecodeerror.py", line 4, in <module>
    json_data=json.loads(f.read())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 65 (char 64)

Solution:

To fix this error, you have to correct the format of the json file. When we have a large file, validating the json file manually might be cumbersome. In such cases, we can use jsonlint to validate the format of the JSON.

If our file has two dictionaries as shown, we see an error. That is because the root element is missing. To fix this, we have to format the file appropriately.

 

json file with proper formatting

 

Let’s run the code and check.

import json

with open("sample.json") as f:
    json_data=json.loads(f.read())
    print(json_data)

Output:

{'data': [{'id': '100110', 'city_id': '101', 'name': 'Alice', 'Accepted': True}, {'id': '100111', 'city_id': '101', 'name': 'Bob', 'Accepted': False}]}

Case 3: The JSON response/file has values that are not understood by the json decoder.

Note that in JSON, boolean expressions are denoted as true and false. If the file has some other values like True and False, the JSON decoder won’t decode them and an error is thrown.

Consider the JSON file below:

 

json file with ununderstood values

 

 

We see an error when we run this file as the JSON decoder doesn’t understand these keywords.

import json

with open("sample.json") as f:
    json_data=json.loads(f.read())
    print(json_data)

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\jsondecodeerror.py", line 4, in <module>
    json_data=json.loads(f.read())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 75 (char 74)

To fix this error, use the right keywords, i.e true and false (all the letters in lowercase) in your json file.

 

corrected json file

 

Now, run the program as shown below

{"data": [{"id": "100110", "city_id": "101", "name": "Alice", "Accepted": true},
{"id": "100111", "city_id": "101", "name": "Bob", "Accepted": false}]}

Output:

{'data': [{'id': '100110', 'city_id': '101', 'name': 'Alice', 'Accepted': True}, {'id': '100111', 'city_id': '101', 'name': 'Bob', 'Accepted': False}]}

Note that, we have specified true and false in the json file. The Json decoder translates it to Python objects as True and False

 

Conclusion:

In this article, we have discussed different reasons to see the json.decoder.JSONDecodeError: Expecting value error and also discussed how to fix the issues in various cases. We hope you’ve found this article informative. Thank you for Reading. Do come back to us to know more on how to resolve errors like this.

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

Leave a Comment