Skip to content

How to Resolve Python "json.decoder.JSONDecodeError: Extra data"

Fix the 'Extra data' JSON parsing error in Python by wrapping multiple objects/arrays in a container, validating JSON structure, or processing line-delimited JSON files.

When parsing JSON data in Python using json.loads() (for strings) or json.load() (for files), you might encounter the json.decoder.JSONDecodeError: Extra data: line X column Y (char Z). This error indicates that the parser successfully read a complete, valid JSON value (like an object {...} or an array [...]) but then found additional, unexpected characters immediately following it within the same input stream or string, which violates the JSON standard.

This guide explains the common causes of this error, primarily related to multiple JSON objects/arrays not being properly contained, and provides standard solutions.

Understanding the Error: JSON Expects a Single Top-Level Value

The JSON specification requires a valid JSON text to consist of one single top-level value. This value can be a simple type (string, number, boolean, null) or a complex type (object {} or array []).

The Extra data error occurs because the Python json parser successfully parsed one complete JSON value but then found more non-whitespace characters immediately after it. The parser doesn't know how to handle this "extra" data because it expects the input to end after the first complete value.

Cause 1: Multiple JSON Objects/Arrays Not Enclosed in a Container

This is the most common cause of the "Extra data" error. You have input that looks like multiple JSON objects or arrays placed next to each other without being contained within a single parent array or object.

Multiple Objects Side-by-Side

  • Error Scenario (String):
    import json
    invalid_json_string = r'{"id": 1, "name": "A"}{"id": 2, "name": "B"}'
    print(f"Input String: {invalid_json_string}")
    try:
        data = json.loads(invalid_json_string)
        print(data)
    except json.JSONDecodeError as e:
        print(e)
  • Error Scenario (File - data.json):
    {"id": 1, "name": "Alice"}
    {"id": 2, "name": "Bob"}

Multiple Arrays Side-by-Side

Error Scenario (String):

import json
invalid_json_string = r'[1, 2][3, 4]' # Two arrays adjacent
print(f"\nInput String: {invalid_json_string}")
try:
    data = json.loads(invalid_json_string)
    print(data)
except json.JSONDecodeError as e:
    print(e)

The standard way to represent a collection of multiple JSON objects or values is to enclose them in a JSON array (square brackets []), with commas separating the elements.

Fixing Strings

import json

valid_json_string_objects = r'[{"id": 1}, {"id": 2}]'
print(f"\nValid Objects String: {valid_json_string_objects}")
data_objects = json.loads(valid_json_string_objects)
print(f"Parsed Objects: {data_objects}")        # Output: [{'id': 1}, {'id': 2}]
print(f"Accessing: {data_objects[0]['id']}")    # Output: 1

valid_json_string_arrays = r'[[1, 2], [3, 4]]'
print(f"\nValid Arrays String: {valid_json_string_arrays}")
data_arrays = json.loads(valid_json_string_arrays)
print(f"Parsed Arrays: {data_arrays}")          # Output: [[1, 2], [3, 4]]
print(f"Accessing: {data_arrays[1][0]}")        # Output: 3

Fixing Files

Modify the .json file content to wrap the multiple objects/arrays in a single top-level array.

  • Corrected data.json (Multiple Objects):
    [
      {"id": 1, "name": "Alice"},
      {"id": 2, "name": "Bob"},
      {"id": 3, "name": "Carl"}
    ]
  • Corrected data.json (Multiple Arrays):
    [
      [1, 2],
      [3, 4],
      [5, 6]
    ]

Now, reading these files with json.load() will work correctly.

import json
import os

filename = "data_fixed.json"

with open(filename, "w", encoding='utf-8') as f:
     f.write('[\n  {"id": 1, "name": "Alice"},\n  {"id": 2, "name": "Bob"}\n]')

try:
    with open(filename, 'r', encoding='utf-8') as f:
        my_data = json.load(f)
        print(f"Data loaded from {filename}:")
        print(my_data) # Output: [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
except FileNotFoundError:
    print(f"Error: File '{filename}' not found.")
except json.JSONDecodeError as e:
    print(f"Error decoding JSON from file: {e}")
finally:
    if os.path.exists(filename): os.remove(filename)

Output:

Data loaded from data_fixed.json:
[{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]

Solution 2: Wrap Multiple Values in a JSON Object {}

Alternatively, you can structure your JSON so the multiple items are values within a single top-level JSON object.

Fixing Strings

import json


valid_object_wrap = r'{"obj1": {"id": 1}, "obj2": {"id": 2}}'
data_obj = json.loads(valid_object_wrap)
print(f"\nParsed Object Wrap: {data_obj}")      # Output: Parsed Object Wrap: {'obj1': {'id': 1}, 'obj2': {'id': 2}}
print(f"Accessing: {data_obj['obj1']['id']}")   # Output: 1

valid_array_wrap = r'{"listA": [1, 2], "listB": [3, 4]}'
data_arr = json.loads(valid_array_wrap)
print(f"\nParsed Array Wrap: {data_arr}")       # Output: Parsed Array Wrap: {'listA': [1, 2], 'listB': [3, 4]}
print(f"Accessing: {data_arr['listB'][0]}")     # Output: 3

Fixing Files

Modify the .json file to have a single top-level object containing the other items.

Corrected data.json:

{
    "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
    ],
    "status": "processed"
}

Solution 3: Process as Line-Delimited JSON (JSON Lines / ndjson)

If your input source intentionally contains one complete, independent JSON object (or value) per line, this is a format known as JSON Lines (.jsonl) or newline-delimited JSON (ndjson). The standard json.load() or json.loads() can not parse this directly because it expects a single JSON value for the whole input.

You need to read the input line by line and parse each line individually.

  • Example data.jsonl:
    {"id": 1, "event": "start"}
    {"id": 2, "event": "process", "value": 10}
    {"id": 3, "event": "end"}
  • Python Code:
    import json
    import os
    
    filename = "data.jsonl"
    with open(filename, "w", encoding='utf-8') as f:
        f.write('{"id": 1, "event": "start"}\n')
        f.write('{"id": 2, "event": "process", "value": 10}\n')
        f.write('{"id": 3, "event": "end"}\n')
    
    parsed_data = []
    print(f"Reading line-delimited JSON from '{filename}':")
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if line:
                    try:
                        obj = json.loads(line)
                        parsed_data.append(obj)
                        print(f"  Parsed line: {obj}")
                    except json.JSONDecodeError as e:
                        print(f"  Error parsing line '{line}': {e}")
        print(f"Final list of parsed objects: {parsed_data}")
    except FileNotFoundError:
         print(f"Error: File '{filename}' not found.")
    except Exception as e:
         print(f"An unexpected error occurred: {e}")
    finally:
        if os.path.exists(filename): os.remove(filename)
    Output:
    Reading line-delimited JSON from 'data.jsonl':
    Parsed line: {'id': 1, 'event': 'start'}
    Parsed line: {'id': 2, 'event': 'process', 'value': 10}
    Parsed line: {'id': 3, 'event': 'end'}
    Final list of parsed objects: [{'id': 1, 'event': 'start'}, {'id': 2, 'event': 'process', 'value': 10}, {'id': 3, 'event': 'end'}]

Checking JSON Validity

If you suspect your JSON might be malformed beyond just the "Extra data" issue, use an online JSON validator or editor linters to pinpoint syntax errors (like missing quotes, incorrect bracket usage, invalid literals).

Conclusion

The json.decoder.JSONDecodeError: Extra data occurs when the JSON parser finds unexpected characters after successfully parsing one complete top-level JSON value (object or array). This usually means your input contains multiple JSON values side-by-side without a proper container.

The standard solutions are:

  1. Wrap the multiple values in a single JSON array [...], separating elements with commas (no trailing comma).
  2. Structure the data within a single top-level JSON object {...}, where the multiple values become properties of that object.
  3. If the input is intentionally line-delimited JSON (.jsonl), read and parse each line individually using json.loads().

Ensuring your input conforms to the JSON standard of having a single top-level value is key to resolving this error.

Navigation