JSON is a standard Python library used to parse the JSON format data received from APIs. The error JSONDecodeError: Expecting value: line 1 column 1 (char 0) occurs when the JSON response is not as expected.
Firstly, let’s look at different causes for this error.
Recommended Reading: How to Fix json.decoder.JSONDecodeError: Expecting value
Table of Contents
What causes the JSONDecodeError: Expecting value: line 1 column 1 (char 0) ?
There are several reasons to see the JSON Decode Error in the program. Some of them are :
- The URL is not accessible.
- The content in the URL is not in JSON format.
- The JSON file is not in a valid format.
- The character encoding of the file is not compatible.
Now, that we have seen different causes of this error, let’s check out different ways to fix JSONDecodeError.
Fix 1: Check if the issue is with the HTTP response
At times, the issue is not really because of the JSON file. But because the URL is not accessible or contains data that is not in JSON format.
Check for the status_code and proceed if it is 200
As we all know, when you request a particular URL, an HTTP response status code is sent indicating whether the request was successful or not. When the HTTP response status codes 404, 500, etc, are returned, you see the JSON decode error.
Problem :
We are trying to access a particular URL, we see a JSON Decode error as the URL cannot be accessed.
import requests import json response = requests.get("https://thexxxxpage.com/wp-xxxxxx/") content=json.loads(response.text)
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 12, in <module> content=json.loads(response.text) 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)
Solution:
To fix this, we can have a simple check and proceed only if the HTTP response status code is 200.
import requests import json response = requests.get("https://thexxxxxpage.com/wp-xxxxxx/") if response.status_code == 200 : content=json.loads(response.text) print(content) print(type(content)) else: print(" There was an error with error code {} while accessing the page".format(response.status_code))
Ensure the Content is in JSON format.
If the Content is not in JSON format, and say is in the HTML, XML, or some other format, this error is seen. To verify this we can check the Content-Type from the header.
Problem:
When we access a URL in HTML format, we see JSONDecodeError as shown below.
response = requests.get("https://pythonhowto.com") if response.status_code == 200 : content=json.loads(response.content) print(content) else: print("There was an error with error code {} while accessing the page".format(response.status_code))
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 13, in <module> content=json.loads(response.content) 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)
Solution:
To correct this, we access the Content-Type from the response header and check if it contains ‘application/json’. We go ahead after validating this.
import requests import json response = requests.get("https://pythonhowto.com") if response.status_code == 200 and 'application/json' in response.headers['Content-Type'] : content=json.loads(response.content) print(content) else : print("There is some error retiveing the URL") print("The HTTP Status code is {} , the content-type is {}".format(response.status_code,response.headers['Content-Type'].split(";")[0]))
Output:
There is some error retiveing the URL The HTTP Status code is 200 , the content-type is text/html
Fix 2: Ensure the JSON file is in the proper format.
If the JSON file you are trying to access is not in the proper format, the error is seen.
Consider the following file, we can see the file is not in proper JSON format. It is missing a lot of ending braces.
Problem:
When you try to access the JSON file with improper formatting, you see the JSONDecodeError. Consider the following example.
import requests import json with open("C:\\Users\\paian\\Downloads\\empty_obj.json") as f : content=json.loads(f.read()) print(content)
Output :
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 5, in <module> content=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 353, in raw_decode obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Solution:
To fix this error, format your file in proper JSON format. If the file is lengthy, use a JSON validator like JSONlint.
After formatting, the error will be fixed.
import requests import json with open("C:\\Users\\paian\\Downloads\\test_obj.json") as f : content=json.loads(f.read()) print(content)
Output:
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON:API', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
Fix 3: Use compatible character encoding format.
Python 3, by default, understands utf-8 format characters. If the file uses, some other encoding schemes, that has to be specified explicitly.
We can specify the encoding scheme as shown below.
with open("C:\\Users\\paian\\Downloads\\empty_obj.json",encoding='utf-16',error='ignore') as f : content=json.loads(f.read()) print(content)
Fix 4: Don’t use json.load() on file paths
When you read the JSON files, don’t use json.load() or json.loads() directly on the file paths.
Problem:
Consider the following example.
import json content=json.loads("C:\\Users\\paian\\Downloads\\empty_obj.json") print(content)
Output:
Traceback (most recent call last): File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\json__learning.py", line 9, in <module> content=json.loads("C:\\Users\\paian\\Downloads\\empty_obj.json") 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)
Solution:
Instead, open the file , read the contents and then apply json.load() or json.loads()
import json with open("C:\\Users\\paian\\Downloads\\empty_obj.json") as f : content=json.loads(f.read()) print(content)
Output:
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON:API paints my bikeshed!', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
Conclusion
In this article, we have discussed different ways to fix the JSONDecodeError: Expecting value: line 1 column 1 (char 0). We hope this article has been informative. Kindly comment and let us know if you found this helpful.
Also, check Attribute Error : ‘str’ object has no attribute ‘decode’ in Python 3
Happy Pythoning.
2 thoughts on “How to Fix JSONDecodeError: Expecting value: line 1 column 1 (char 0)”