Unexpected non-whitespace character after JSON at position N
JSON.parse reads exactly one value. It read one, the value was fine, and then it found more text. Your first value is not broken. Something follows it, and the position tells you exactly where that something starts.
How the message appears
Chrome and Node first, then the older V8 wording, then Firefox, then Safari. Newer V8 adds the line and column after the position. Safari does not say what it found, only that it failed.
The fix
Work out which format you have. If it is JSON Lines, parse one line at a time with text.split("\n").filter((line) => line.trim()).map((line) => JSON.parse(line)). If it should be one document, wrap the values in [ ] with commas between them, as the fixed example does. If it is one stray character, delete it.
{"id": 1, "status": "ok"}
{"id": 2, "status": "ok"}[
{"id": 1, "status": "ok"},
{"id": 2, "status": "ok"}
]The example travels in the URL fragment, which browsers never send to a server.
What causes it
Two JSON documents in one string
Two responses written into one buffer, a file that was appended to, or two JSON.stringify results joined together. The position lands on the second {, and each half parses on its own.
It is JSON Lines, not JSON
One object per line, no commas and no surrounding array. Logs, exports and streaming APIs use this format on purpose, often in .jsonl or .ndjson files. Nothing is wrong with the text. It needs a different reader.
A stray character at the end
A trailing comma or semicolon, one closing brace too many, or text that a template added after the JSON. {"a": 1}; fails at position 8, which is the semicolon.
A brace that closed the document early
An extra } in the middle ends the top-level object before you meant it to, and everything after it counts as extra. In {"a": {"b": 1}}, "c": 2} the position points at the comma, but the mistake is the brace just before it.
Worth knowing
- Check what reads the file before you turn JSON Lines into an array. The format exists so a large file can be processed one line at a time, and an array has to be held in memory whole.
- Python reports the same thing as
Extra data: line 1 column N (char N), and that page covers the Python side.
Written by Vishnu Shankar.