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


Borislav Hadzhiev

Sun Apr 24 20223 min read


Photo by Emma Peneder

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

The Python "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" occurs when we try to parse something that is not valid JSON as if it were. To solve the error, make sure the response or the file is not empty or conditionally check for the content type before parsing.

Here is a very simple example of how the error occurs.

import json # ⛔️ json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) result = json.loads('')

We are trying to parse an empty string as if it were valid JSON.

The most common causes of the error are:

  • Getting an empty response from a remote server (e.g. 204 or 404) and trying to parse it as if it were JSON
  • Trying to parse a response with a different content-type (e.g. text/html) as if it were JSON
  • Trying to read a JSON file incorrectly or trying to parse the contents of an empty JSON file

If you are getting the error while making an API request, make sure the response has an application/json content-type header before parsing it.

import requests def make_request(): response = requests.delete('https://reqres.in/api/users/2') print('response: 👉️', response) # response: 👉️ <Response [204]> print('response.text: 👉️', response.text) # response.text: 👉️ "" # response.status_code: 👉️ 204 print('response.status_code: 👉️', response.status_code) print('response.headers: 👉️', response.headers) if (response.status_code != 204 and 'content-type' in response.headers and 'application/json' in response.headers['content-type']): parsed = response.json() print('✅ parsed response: 👉️', parsed) else: # 👇️ this runs print('⛔️ conditions not met') make_request()

The example uses the requests package and makes an HTTP DELETE request which returns a 204 status (no content).

Trying to parse an empty response as if it were JSON would raise a JSONDecodeError, so we have to check if the response status is not 204, the response headers dict has a content-type key and the value of the `content-type` header is application/json.

This way we can be sure that the server has sent us a valid JSON response before trying to parse it with the reponse.json() method (if using requests) or with json.loads(my_json_str).

If the server sent you an empty response or the response if not of type application/json you would get a JSONDecodeError.

You can't try to parse a text/html response (or an empty response) as if it were JSON.

The error is often caused when trying to read a JSON file incorrectly or trying to read a JSON file that is empty or contains invalid JSON.

You can use the json.load() method to deserialize a JSON file to a Python object.

import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_data = json.load(f) print(my_data) # 👉️ {'name': 'Alice', 'age': 30}

The example assumes that there is a file called example.json located in the same directory.

{"name": "Alice", "age": 30}

Make sure that the file that you are reading from is not empty as that often causes the error.

Another common cause of the error is passing the filename to the json.loads() method when trying to read from a JSON file.

The json.load method is used to deserialize a file to a Python object, whereas the json.loads method is used to deserialize a JSON string to a Python object.

The json.load() method expects a text file or a binary file containing a JSON document that implements a .read() method.

Alternatively, you manually call the read() method on the file object and use the json.loads() method.

import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: # 👇️ make sure to call read() my_data = json.loads(f.read()) print(my_data) # 👉️ {'name': 'Alice', 'age': 30} print(type(my_data)) # 👉️ <class 'dict'>

The example above achieves the same result, but instead of relying on the json.load() method to call read() on the file object for us, we manually do it and use the json.loads() method instead.

The json.loads() method basically helps us load a Python native object (e.g. a dictionary or a list) from a JSON string.

The JSONEncoder class supports the following objects and types by default.

list, tuplearray
int, float, int and float derived Enumsnumber
Use the search field on my Home Page to filter through my more than 1,000 articles.