JSONDecodeError: Expecting value: line 1 column 1 (char 0) error typically occurs when you “parse a string as JSON, but the string is not in a valid JSON format.”
Common reasons
- Empty Input: The JSON data you’re trying to decode is an empty string.
- Invalid Input: The data you’re trying to decode isn’t JSON-formatted.
- Source Issues: If you’re fetching the JSON from a URL or file, there might be an issue with the source. The URL might not return any data, or the file might be empty.
- Empty json file: Trying to misread a JSON file or trying to parse the contents of an empty JSON file.
How to Fix
- Check the JSON: Ensure your decoding JSON data isn’t empty. If it’s a file, open it and check its contents.
- Validate the JSON: Use a JSON validator tool to properly format the JSON data.
- Logging: If you’re fetching the JSON dynamically (e.g., from a URL), logging the response content might be a good idea to see what you’re getting.
- Ensure that the input is a string because the json.loads() function can only work on strings.
Issue 1: Decoding empty json
import json
result = json.loads('')
print(result)
Output
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Fix 1
import json
main_str = ''
try:
result = json.loads(main_str)
except json.decoder.JSONDecodeError:
print('The string is empty or does NOT contain valid JSON')
Output
The string is empty or does NOT contain valid JSON
Issue 2: Decoding invalid JSON content
Python json library requires you to pass valid JSON content when calling the load() or loads() function.
import json
data = '{"name": Nathan}'
result = json.loads(data)
print(result)
Output
json.decoder.JSONDecodeError: Expecting value: line 1 column 10 (char 9)
Fix 2
You can use a try-except
block to check if your data is a valid JSON string like this:
import json
data = '{"name": Nathan}'
try:
result = json.loads(data)
except json.decoder.JSONDecodeError:
print('The string does NOT contain valid JSON')
Output
The string does NOT contain valid JSON
Issue 3: Empty or invalid JSON file
Another case when this error may happen is when you load an empty .json file.
import json
with open("data.json", "r") as file:
data = json.loads(file.read())
print(data)
Output
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Fix 3
import json
try:
with open("data.json", "r") as file:
data = json.loads(file.read())
print(data)
except json.decoder.JSONDecodeError:
print("File is empty or contain invalid JSON")
Output
File is empty or contain invalid JSON
Issue 4: The server didn’t return a valid JSON
When you send an HTTP request using the requests library, you can “use the .json()” method from the response object to extract the JSON content:
import requests
response = requests.get('https://api.github.com')
data = response.json()
print(data)
Output
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1
Fix 4
To fix this error, you need to surround the call to response.json() with a try-except block:
import requests
response = requests.get('https://api.github.com')
try:
data = response.json()
print(data)
except:
print("Error from server: " + str(response.content))
When the except block is triggered, the response content will be printed in a string format.
That’s it!
Related posts
Pylint unresolved import error in Python
IndentationError: expected an indented block
ValueError: i/o operation on closed file

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.