json.decoder.JSONDecodeError: Extra data in Python [Solved]

avatar

Borislav Hadzhiev

4 min

banner

Photo from Unsplash

# 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.

jsondecodeerror extra data line 1

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

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

parsing multiple objects

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

There are 3 main ways to solve the error:

  1. Wrap the objects in an array.
  2. Set an array property on the object that contains the multiple objects.
  3. Iterate over the objects and parse each object.

# Wrap the JSON objects in a List to solve the error

One way to solve the error is to wrap the objects in an array and separate them by a comma.

main.py
# ✅ an array of objects import json result = json.loads(r'[{"id": 1}, {"id": 2}]')

Notice that we wrapped the objects in square brackets to create a JSON array.

Make sure to separate the objects in the array by a comma.

However, they shouldn't be a comma after the last object.

main.py
import json # ✅ correct (no trailing comma) a_list = json.loads(r'[{"id": 1}, {"id": 2}]') # ⛔️ incorrect (has trailing comma) a_list = json.loads(r'[{"id": 1}, {"id": 2},]')

If you add a comma after the last object, a parsing error is raised.

# Solve the error when reading from a JSON file

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)

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}

Having multiple objects that are not wrapped in an array is not valid JSON.

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} ]

We create a list by wrapping the objects in square brackets.

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) print(my_data[0]['id']) # 👉️ 1 print(my_data[0]['name']) # 👉️ Alice

Having an array of comma-separated objects is perfectly valid JSON, so everything works as expected.

# Add an array property to the JSON object

Alternatively, you can add a new property to 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)

The dictionary has an employees key that is an array of objects.

# Parsing each individual line in a list comprehension

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 as 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.

# Having multiple arrays next to one another

The error is also raised if you have multiple arrays next to one another without them being wrapped in a another array.

main.py
import json # ⛔️ json.decoder.JSONDecodeError: Extra data: line 1 column 11 (char 10) result = json.loads(r'["a", "b"]["c", "d"]')

One way to solve the error is to add the elements to a single array.

main.py
import json result = json.loads(r'["a", "b", "c", "d"]') print(result) # 👉️ ['a', 'b', 'c', 'd'] print(result[0]) # 👉️ a print(result[1]) # 👉️ b

Notice that we used only one set of square brackets.

Another way to solve the error is to use a two-dimensional array.

main.py
import json result = json.loads(r'[["a", "b"], ["c", "d"]]') print(result) # 👉️ [['a', 'b'], ['c', 'd']] print(result[0]) # 👉️ ['a', 'b'] print(result[1]) # 👉️ ['c', 'd'] print(result[0][0]) # 👉️ a print(result[1][0]) # 👉️ c

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(a_list) - 1.

A third way to solve the error is to add the arrays as properties on an object.

main.py
import json result = json.loads(r'{"first": ["a", "b"], "second": ["c", "d"]}') # 👇️ {'first': ['a', 'b'], 'second': ['c', 'd']} print(result) print(result['first']) # 👉️ ['a', 'b'] print(result['second']) # 👉️ ['c', 'd'] print(result['first'][0]) # 👉️ a print(result['second'][0]) # 👉️ c

The arrays are now values of the first and second properties, so the string is valid JSON.

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.