JSONDecodeError: Extra data: How to Fix It in Python
JSONDecodeError: Extra data means json.loads() found more text after the first complete JSON value. Here is why it happens in Python and how to fix it.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →JSONDecodeError: Extra data means Python parsed one complete JSON value and then found more characters after it. A JSON document holds a single top-level value, so json.loads() rejects anything that follows.
What “JSONDecodeError: Extra data” means
The error means json.loads() read one full JSON value and then hit more non-whitespace text. The JSON standard defines a JSON text as one value with optional whitespace around it. Python's parser stops at the end of the first value and checks the rest. If anything is left, it raises Extra data and reports the line, column and character offset where the leftover text starts.
Causes and the fix for each
Two objects are joined in one string
Two complete values sit side by side. The parser reads the first and fails on the second.
Before (fails):{"a": 1} {"b": 2}
After (valid):
[{"a": 1}, {"b": 2}]
The file is JSON Lines, one object per line
Each line is valid alone, but the whole file is not one JSON value. Passing it to json.loads() fails at the start of line 2.
Before (fails):{"id": 1}
{"id": 2}
{"id": 3}
After (valid):
[{"id": 1}, {"id": 2}, {"id": 3}]
Objects are separated by commas without an outer array
The comma is not allowed after a top-level value. The error points at the comma.
Before (fails):{"id": 1},{"id": 2}
After (valid):
[{"id": 1},{"id": 2}]
A stray closing brace or bracket ends the document early
An extra closer makes the first value end sooner than intended, and the leftover character is extra data.
Before (fails):{"user": {"id": 1}}}
After (valid):
{"user": {"id": 1}}
A file was written with json.dump() in append mode
Each run adds a new document after the old one. The file now holds several top-level values.
Before (fails):{"run": 1}{"run": 2}
After (valid):
[{"run": 1}, {"run": 2}]
An LLM or API added a second value after the JSON
A model reply can contain the JSON followed by another object or a repeated copy. Only the first value is valid on its own.
Before (fails):{"ok": true}
{"ok": true}
After (valid):
{"ok": true}
How to fix it in Python
- Read the position in the message. Extra data: line 1 column 10 (char 9) tells you where the leftover text starts. Print text[pos:pos+40] to see it.
- If the leftover is another JSON value on a new line, the data is JSON Lines. Parse it line by line with json.loads() on each non-empty line.
- If several values are joined without separators, use json.JSONDecoder().raw_decode() in a loop. It returns the value and the index where it stopped.
- If the leftover is a comma, a stray bracket or repeated text, fix the source that produced it. Wrap comma-separated objects in [ ] or remove the extra character.
- If the file is written by your own code, stop appending with json.dump(). Write one object per line with a newline, or load, extend and rewrite a single list.
- If the text is large or comes from a model, paste it into the Repair tool to see the broken spot, then check the result with the Validator.
import json
def load_all(text):
"""Parse one or more concatenated JSON values. Returns a list."""
decoder = json.JSONDecoder()
text = text.strip()
pos = 0
items = []
while pos < len(text):
try:
obj, end = decoder.raw_decode(text, pos)
except json.JSONDecodeError as exc:
raise ValueError(f"Bad JSON at char {pos}: {exc.msg}") from exc
items.append(obj)
pos = end
while pos < len(text) and text[pos].isspace():
pos += 1
return items
data = load_all('{"a": 1} {"b": 2}')
print(data) # [{'a': 1}, {'b': 2}]
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Extra data: line 1 column 10 (char 9) | A second JSON value follows the first on the same line | Use raw_decode() in a loop, or put the values in an array |
| Extra data: line 2 column 1 (char 10) | JSON Lines file passed to json.loads() | Call json.loads() on each line |
| Extra data: line 1 column 9 (char 8) | A comma or stray bracket right after the first value | Wrap comma-separated objects in [ ] or remove the extra character |
| Extra data: line 5 column 1 (char 74) | Text or a second value after the JSON in a longer document | Cut the text at the offset or parse it with raw_decode() |
| requests.exceptions.JSONDecodeError: Extra data | response.json() received a body with several JSON values | Read response.text and parse it line by line or with raw_decode() |
Related errors
- Fix Python JSON Errors: json.loads() Decode Errors and How to Repair Them
- Invalid JSON Meaning: What It Is and How to Fix It
- JSONDecodeError: Expecting ',' delimiter: How to Fix It in Python
- Expecting property name enclosed in double quotes (Python Fix)
- Why LLMs Output True Instead of true (and How to Fix It)
Key takeaways
- Extra data means a full JSON value was parsed and more text followed.
- The char offset in the message shows where the leftover starts.
- JSON Lines files need line-by-line parsing, not one json.loads() call.
- raw_decode() parses joined values one at a time.
- Fix the writer if it appends documents to the same file.
FAQ
What does the char number in Extra data mean?
It is the zero-based index in the string where the leftover text starts. Slice the string at that index to see what the parser did not accept.
Can json.loads() read several JSON objects at once?
No. It accepts one top-level value. Use raw_decode() in a loop for joined values, or split a JSON Lines file by line.
Is a JSON Lines file valid JSON?
The whole file is not. Each line is a valid JSON text on its own, but the file as a whole holds many top-level values, so json.loads() raises Extra data.
Why does Extra data appear when a file looks fine?
Check for a second object, a trailing comma-separated item or an extra closing brace at the end of the file. Hidden leftovers from appended writes are common.
Does trailing whitespace cause Extra data?
No. Whitespace around a JSON value is allowed. Only non-whitespace characters after the value raise the error.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles