Extra data: line 1 column N (char N)
json.loads reads exactly one JSON value and then stops. This message means it read one just fine and found more text sitting after it. Your file is not broken. It is two documents where the parser expected one, and the offset tells you where the second one starts.
How the message appears
CPython's json module reports line, column and character offset. Here the offset is where the second document starts, which is the most useful number on this page.
The fix
Work out which format you actually have. If it is JSON Lines, parse one line at a time with [json.loads(line) for line in f if line.strip()]. If it really should be one document, wrap the values in [ ] and put commas between them, the way the fixed example does.
{"id": 1, "name": "Ada"}
{"id": 2, "name": "Grace"}[
{"id": 1, "name": "Ada"},
{"id": 2, "name": "Grace"}
]The example travels in the URL fragment, which browsers never send to a server.
What causes it
It is JSON Lines, not JSON
One complete JSON object per line, no commas, no wrapping array. .jsonl and .ndjson files are built this way on purpose, and so is the output of most log pipelines and a lot of streaming APIs. Nothing is wrong with the file. It just needs a different reader.
A file that was appended to
A script that opens the file with mode a and calls json.dump on every run stacks one document on top of another. Each one is valid on its own. The file as a whole is not a single value.
Two responses concatenated
A proxy or a retry wrote both bodies into the same buffer. The offset lands exactly on the second {, and that is how you tell this apart from a syntax error inside one document.
Worth knowing
- Check what reads the file before you convert a
.jsonlinto an array. JSON Lines exists so a huge file can be streamed one record at a time instead of held in memory all at once. Wrapping it in an array throws that away. json.JSONDecoder().raw_decode(text)parses one value and hands back the index it stopped at. That is the supported way to walk a stream of documents stacked end to end.
Written by Vishnu Shankar.