Last updated: Apr 8, 2024
Reading time·4 min
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.
import json # ⛔️ json.decoder.JSONDecodeError: Extra data: line 1 column 10 (char 9) result = json.loads(r'{"id": 1}{"id": 2}')
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:
One way to solve the error is to wrap the objects in an array and separate them by a comma.
# ✅ 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, there shouldn't be a comma after the last object.
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.
Here is an example of how the error occurs while reading a file.
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.
{"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.
[ {"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.
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.
Alternatively, you can add a new property to the JSON object.
{ "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.
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.
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.
{"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.
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.
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.
The error is also raised if you have multiple arrays next to one another without them being wrapped in another array.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: