- JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- 1. Decoding invalid JSON content
- 2. You’re loading an empty or invalid JSON file
- 3. A request you made didn’t return a valid JSON
- Conclusion
- Take your skills to the next level ⚡️
- About
- Search
- Tags
- How To Fix “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” Error
- JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- Reproduce The Error
- Explain The Error
- Solutions
- Summary
- Maybe you are interested in similar errors:
- Saved searches
- Use saved searches to filter your results more quickly
- json() crashes interpreter with a JSONDecodeError #4291
- json() crashes interpreter with a JSONDecodeError #4291
- Comments
- Expected Result
- Actual Result
- Reproduction Steps
- System Information
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
A JSONDecodeError: Expecting value: line 1 column 1 (char 0) when running Python code means you are trying to decode an invalid JSON string.
This error can happen in three different cases:
Case 1: Decoding invalid JSON content Case 2: Loading an empty or invalid .json file Case 3: A request you made didn’t return a valid JSON
The following article shows how to resolve this error in each case.
1. Decoding invalid JSON content
The Python json library requires you to pass valid JSON content when calling the load() or loads() function.
Suppose you pass a string to the loads() function as follows:
Because the loads() function expects a valid JSON string, the code above raises this error:
To resolve this error, you need to ensure that the JSON string you passed to the loads() function is valid.
You can use a try-except block to check if your data is a valid JSON string like this:
' By using a try-except block, you will be able to catch when the JSON string is invalid.
You still need to find out why an invalid JSON string is passed to the loads() function, though.
Most likely, you may have a typo somewhere in your JSON string as in the case above.
Note that the value Nathan is not enclosed in double quotes:
If you see this in your code, then you need to fix the data to conform to the JSON standards.
2. You’re loading an empty or invalid JSON file
Another case when this error may happen is when you load an empty .json file.
Suppose you try to load a file named data.json file with the following code:
If the data.json file is empty, Python will respond with an error:
The same error also occurs if you have invalid JSON content in the file as follows:
To avoid this error, you need to make sure that the .json file you load is not empty and has valid JSON content.
You can use a try-except block in this case to catch this error:
If you want to validate the source file, you can use jsonlint.com.
3. A request you made didn’t return a valid JSON
When you send an HTTP request using the requests library, you may use the .json() method from the response object to extract the JSON content:
But if the response object doesn’t contain a valid JSON encoding, then a JSONDecodeError will be raised:
As you can see, the requests object also has the JSONDecodeError: Expecting value line 7 column 1 message.
To resolve this error, you need to surround the call to response.json() with a try-except block as follows:
When the except block is triggered, you will get the response content printed in a string format.
You need to inspect the print output for more insight as to why the response is not a valid JSON format.
Conclusion
In this article, we have seen how to fix the JSONDecodeError: Expecting value error when using Python.
This error can happen in three different cases: when you decode invalid JSON content, load an empty or invalid .json file, and make an HTTP request that doesn’t return a valid JSON.
By following the steps in this article, you will be able to debug and fix this error when it occurs.
Until next time, happy coding! 🙌
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with:
How To Fix “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” Error
The error “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” can happen when you working with Python. Learn why Python raises this exception and how you can resolve it.
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Reproduce The Error
Python provides a built-in module for encoding and decoding JSON contents, which can be strings or files. The loads() is one of the most popular functions in this module. However, it raises a JSONDecodeError instead:
>>> import json >>> json.loads(“”) Traceback (most recent call last): File “/usr/lib/python3.10/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)
When you read an empty JSON file with load() , Python will also print out a similar error message:
>>> import json >>> with open("empty.json", "r") as emptyJSON: . print(json.load(emptyJSON)) . Traceback (most recent call last): File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
You can run into this error outside the json module as well. Requests is a popular library for dealing with HTTP requests in Python. It supports the json() method, which converts the fetched content from a resource URI into a JSON object. But it can unexpectedly raise the JSONDecodeError exception:
>>> import requests >>> URL="http://httpbin.org/status/200" >>> response = requests.delete(URL) >>> print(response.json()) During handling of the above exception, another exception occurred: Traceback (most recent call last): … raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Explain The Error
The first two examples are obvious. The error message is displayed in those cases because you provide load() and loads() with empty JSON content.
When those methods fail to deserialize and parse a valid JSON document from those sources, they raise the JSONDecodeError exception.
From this clue, we can guess why the response.json() method in the third example also produces the same error.
There is a high chance the response object itself is empty. This is indeed true, which can be verified by using response.text to get the content of the response:
This is a common response message when we send an HTTP DELETE request to delete a resource. This can be the case even with the status code 200 (meaning the deleting action has been carried out).
Solutions
Refraining from parsing empty files and strings as JSON documents is an obvious fix. But in many situations, we can’t control whether a data source can be empty or not.
Python, however, allows us to handle exceptions like JSONDecodeError. For instance, you can use the try-catch statements below to prevent error messages in the case of empty strings and files:
try: json.loads("") except json.decoder.JSONDecodeError: print("")
try: with open("empty.json", "r") as emptyJSON: print(json.load(emptyJSON)) except json.decoder.JSONDecodeError: print("")
Those snippets execute the loads() and load() functions. If the JSONDecodeError exception is raised, Python executes the except clause and prints an empty string instead of an error message.
We can apply the same idea when parsing the content of an HTTP request response:
try: URL = "http://httpbin.org/status/200" response = requests.delete(URL) print(response.json()) except requests.exceptions.JSONDecodeError: print("")
It is important to note that you must change the exception to catch from json.decoder.JSONDecodeError to requests.exceptions.JSONDecodeError .
Summary
Python throws the error “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” when it parses an empty string or file as JSON. You can use a try statement to catch and handle this exception.
Maybe you are interested in similar errors:
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json() crashes interpreter with a JSONDecodeError #4291
json() crashes interpreter with a JSONDecodeError #4291
Comments
I have reproduced this with requests v. 2.18.4 and 2.14.2, along with Python 3.6.1 and 3.6.2. I have used my system-installed Python/requests (macOS Sierra with Python 3.6.1 downloaded from python.org), Python / requests installed as part of conda 4.3.25, and the latest Python and requests installed from conda-forge.
Expected Result
Expected to see a dictionary of JSON values, as indicated here: http://docs.python-requests.org/en/master/
Actual Result
Python interpreter crashes.
Reproduction Steps
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 23:01:38) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> r = requests.get('https://pypi.python.org/pypi') >>> r.status_code 200 >>> r.encoding 'utf-8' >>> r.json() Traceback (most recent call last): File "", line 1, in File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/site-packages/requests/models.py", line 892, in json return complexjson.loads(self.text, **kwargs) File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Users/psimpson/miniconda3/envs/test/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
System Information
< "chardet": < "version": "3.0.4" >, "cryptography": < "version": "2.0.3" >, "idna": < "version": "" >, "implementation": < "name": "CPython", "version": "3.6.2" >, "platform": < "release": "16.7.0", "system": "Darwin" >, "pyOpenSSL": < "openssl_version": "100020cf", "version": "17.2.0" >, "requests": < "version": "2.18.4" >, "system_ssl": < "version": "100020cf" >, "urllib3": < "version": "1.22" >, "using_pyopenssl": true >
The text was updated successfully, but these errors were encountered: