Solve - json.decoder.JSONDecodeError: Extra data in Python

avatar

Borislav Hadzhiev

Last updated: Apr 24, 2022

banner

Photo from Unsplash

Solve - json.decoder.JSONDecodeError: Extra data in Python #

The Python "json.decoder.JSONDecodeError: Extra data" occurs when we try to parse multiple objects without wrapping them in an array. To solve the error, wrap the JSON objects in an array or declare a new property that points to an array value that contains the objects.

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

main.py
import json # ⛔️ json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) result = json.loads('{}{}')

We are trying to parse 2 objects next to one another without the objects being elements in an array.

Here is an example of how the error occurs while reading a file.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: # ⛔️ json.decoder.JSONDecodeError: Extra data: line 2 column 3 (char 42) my_data = json.load(f) print(my_data) # 👉️ {'name': 'Alice', 'age': 30}

Here is the content of the example.json file.

example.json
{"id": 1, "name": "Alice", "age": 30} {"id": 2, "name": "Bob", "age": 35} {"id": 3, "name": "Carl", "age": 40}

One way to solve the issue is to wrap the JSON objects in an array and use commas to separate the elements.

example.json
[ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 35}, {"id": 3, "name": "Carl", "age": 40} ]

Notice that there isn't a comma after the last element in the array.

Now we can read the file without getting an error.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_data = json.load(f) # 👇️ [{'id': 1, 'name': 'Alice', 'age': 30}, {'id': 2, 'name': 'Bob', 'age': 35}, {'id': 3, 'name': 'Carl', 'age': 40}] print(my_data)

Alternatively, you can add a new property in the json object.

example.json
{ "employees": [ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 35}, {"id": 3, "name": "Carl", "age": 40} ] }

Now parsing the JSON file would get us a Python dictionary.

main.py
import json file_name = 'example.json' with open(file_name, 'r', encoding='utf-8') as f: my_data = json.load(f) # 👇️ {'employees': [{'id': 1, 'name': 'Alice', 'age': 30}, {'id': 2, 'name': 'Bob', 'age': 35}, {'id': 3, 'name': 'Carl', 'age': 40}]} print(my_data)

If you have no control over the JSON data, try using a list comprehension and parse each individual line.

Imagine that we have the following JSON.

main.py
{"id": 1, "name": "Alice", "age": 30} {"id": 2, "name": "Bob", "age": 35} {"id": 3, "name": "Carl", "age": 40}

Here is a list comprehension that parses each individual line.

main.py
import json data = [json.loads(line) for line in open('example.json', 'r', encoding='utf-8')] # 👇️ [{'id': 1, 'name': 'Alice', 'age': 30}, {'id': 2, 'name': 'Bob', 'age': 35}, {'id': 3, 'name': 'carl', 'age': 40}] print(data)

We use the json.loads method to parse each line into a native Python object and add the lines to a list.

However, this would only work if each line contains valid JSON.

The fastest way to validate and correct your JSON is to use a JSON validator.

Paste your payload into the form, the validator checks for errors and sometimes directly fixes them.

If you are fetching data from a remote API, then you have to look into the data the API returns and correct the issue on the backend.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.