How To Fix “JSONDecodeError: Expecting value: line 1 column 1 (char 0)” Error

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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:

print(response.text)

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:

Example 1

try:
	json.loads("")
except json.decoder.JSONDecodeError:
	print("")

Example 2

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:

Leave a Reply

Your email address will not be published. Required fields are marked *