json.decoder.JSONDecodeError: Extra data

Are you getting the error “json.decoder.JSONDecodeError: Extra data“? Well, you have come to the right place. Today in this tutorial, I will show you how to solve this error.

The error “jsondecodeerror extra data” occurs when we parse multiple objects without wrapping them into a single array.

[Fixed]: ModuleNotFoundError: No module named ‘webdriver_manager’

[Fixed]: json.decoder.jsondecodeerror: extra data

We can fix this error “json.decoder.JSONDecodeError: Extra data” by several methods. We will discuss them one by one in detail.

Here is the Python code, how this error occurs:

import json

# json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)
final_response = json.loads('{}{}')

In the above code, we got an error because we are trying to parse two objects without wrapping them into a single Python list.

We may also face this error while reading the JSON file. Let’s see how we can get this error.

Here is the content of the JSON file:

{"id": 1, "name": "Ehtisham", "age": 5}
{"id": 2, "name": "Ali", "age": 18}
{"id": 3, "name": "Awan", "age": 23}

Now let’s try to read this file and see how we will get this error.

import json

file_name = 'myJsonFile.json'

with open(file_name, 'r', encoding='utf-8') as f:
    # json.decoder.JSONDecodeError: Extra data: line 2 column 3 (char 42)
    json_loaded = json.load(f)

[Solution] Fix the error “jsondecodeerror extra data” using the Array

One way to solve this error is to wrap the objects in a single array and use commas between the elements. Like this:

[{"id": 1, "name": "Ehtisham", "age": 5},
{"id": 2, "name": "Ali", "age": 18},
{"id": 3, "name": "Awan", "age": 23}]

Now you can read this file without getting any errors.

import json

file_name = 'myJsonFile.json'

with open(file_name, 'r', encoding='utf-8') as f:
    # json.decoder.JSONDecodeError: Extra data: line 2 column 3 (char 42)
    json_loaded = json.load(f)
print(json_loaded)

#OUTPUT: [{"id": 1, "name": "Ehtisham", "age": 5},{"id": 2, "name": "Ali", "age": 18},{"id": 3, "name": "Awan", "age": 23}]

[Solved] jsondecodeerror extra data Using a New Property

You can also solve this error using a new property in your JSON object. You can do something like this:

{
  "students": [
    {"id": 1, "name": "Ehtisham", "age": 5},
    {"id": 2, "name": "Ali", "age": 18},
    {"id": 3, "name": "Awan", "age": 23}
  ]
}

Now parsing the JSON will give us a Python Dictionary.

import json

file_name = 'myJsonFile.json'

with open(file_name, 'r', encoding='utf-8') as f:
    # json.decoder.JSONDecodeError: Extra data: line 2 column 3 (char 42)
    json_loaded = json.load(f)
print(json_loaded)

#OUTPUT: { "students": [ {"id": 1, "name": "Ehtisham", "age": 5}, {"id": 2, "name": "Ali", "age": 18}, {"id": 3, "name": "Awan", "age": 23}]}

Conclusion on json.decoder.JSONDecodeError: Extra data

Programmers, we discussed how we can solve this error. If you are still getting the error please let us know in the comments section. You can solve this error is to wrap the objects in a single array and use commas between the elements.

Leave a Reply