Python json.loads shows ValueError Extra data
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The ValueError: Extra data error occurs when json.loads() receives a string containing more than one JSON value. The JSON spec defines a single root value per document, so if your string has multiple JSON objects concatenated together (common in log files, streaming APIs, or JSONL formats), the parser fails after successfully reading the first value. The fix depends on the format: split on newlines for JSONL, wrap in an array, or use a streaming decoder.
The Error
The parser successfully reads {"name": "Alice"} but then finds unexpected data starting at character 17 — the second {. JSON only allows one root value per string.
Cause 1: Multiple JSON Objects (JSONL / Newline-Delimited JSON)
The most common source — a file with one JSON object per line:
Cause 2: Concatenated API Responses
Some streaming APIs return multiple JSON objects without delimiters:
raw_decode() parses one JSON value and returns the position where it stopped, letting you continue parsing the rest.
Cause 3: Trailing Content
Extra whitespace, comments, or garbage after valid JSON:
Cause 4: json.load() vs json.loads()
Solution: Parse JSONL Files Robustly
Solution: Wrap Concatenated Objects in an Array
If you control the data format, wrap objects in an array:
This approach is fragile — it breaks if the JSON values contain }{ inside strings. Use raw_decode() for robust parsing.
Using pandas for JSONL
Common Pitfalls
- Confusing JSON and JSONL: A
.jsonfile should contain one JSON value. A.jsonlfile has one JSON object per line. Usingjson.load()on a JSONL file causesExtra data. replace('}{', '},{')hack: Breaks if a string value contains}{. Useraw_decode()or line-by-line parsing instead.- Empty lines in JSONL:
json.loads("")raisesJSONDecodeError. Always strip and skip empty lines. - BOM (byte order mark): Files saved with UTF-8 BOM have
\ufeffat the start. Useopen(file, encoding='utf-8-sig')to strip it automatically. - Streaming APIs: Libraries like
requestscan stream JSON — useresponse.iter_lines()withjson.loads()per line instead ofresponse.text.
Summary
ValueError: Extra datameans the string contains more than one JSON value- For JSONL (one object per line): parse each line separately with
json.loads(line) - For concatenated objects: use
json.JSONDecoder().raw_decode()to parse sequentially - Use
pandas.read_json(file, lines=True)for JSONL files in data workflows - Always validate your JSON format —
json.loads()expects exactly one root value
Related reading
- Python k-means algorithm
- Python K-means fails to fit data when over 100 samples
- Python Kafka multiprocess vs thread
- Python Keras An layer output exactly the same thing as input
- Python kernel dies on Jupyter Notebook with tensorflow 2
- Python kernel dies when importing tensorflow 1.7
- Python Keras LSTM learning converges too fast on high loss
- Python librdkafka producer perform against the native Apache Kafka Producer
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.