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 jsoninvalid_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)
import jsoninvalid_json_string = r'[1, 2][3, 4]' # Two arrays adjacentprint(f"\nInput String: {invalid_json_string}")try: data = json.loads(invalid_json_string) print(data)except json.JSONDecodeError as e: print(e)
Solution 1: Wrap Multiple Values in a JSON Array [] (Recommended)
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.
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.
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:
Wrap the multiple values in a single JSON array [...], separating elements with commas (no trailing comma).
Structure the data within a single top-level JSON object {...}, where the multiple values become properties of that object.
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.