Unexpected Non-Whitespace Character After JSON: Causes and Fixes
Unexpected non-whitespace character after JSON at position N (and Python's Extra data, Gson's JSON document was not fully consumed, .NET's Additional text encountered) means text follows a complete JSON value. Here is why and how to fix it.
Locate the extra textWhat the parser is telling you
A JSON document is exactly one value. The parser read a complete object or array, then found more characters that are not whitespace. Everything up to the reported position was fine; the problem is what comes after it.
Different runtimes report the same situation. JavaScript (V8): Unexpected non-whitespace character after JSON at position N. Firefox: unexpected non-whitespace character after JSON data. Python: Extra data. Java Gson: JSON document was not fully consumed. .NET Newtonsoft: Additional text encountered after finished reading JSON content. System.Text.Json: '{' is invalid after a single JSON value. Expected end of data.
Common causes
Look at the character at the reported position. It almost always falls into one of these cases.
JSON.parse('{"a":1}{"b":2}');
// SyntaxError: Unexpected non-whitespace character after JSON at position 7- Two objects back to back, such as {"a":1}{"b":2}, from appending to a log file or concatenating responses.
- JSON Lines or NDJSON: one object per line, which is a valid format but not a single JSON document.
- An extra closing brace or bracket left over after an edit, such as }}.
- A trailing fragment like a semicolon, a comment, or a debug print written to the same output stream.
- A JSONP or security prefix, such as callback( ... ) or )]}', around the JSON.
Parse JSON Lines one line at a time
If each line is a separate record, split on newlines and parse each non-empty line. If you control the writer and need one document, write a single array instead.
const records = text
.split(/\r?\n/)
.filter(line => line.trim())
.map(line => JSON.parse(line));Fix stray characters and broken writers
For a hand-edited file, delete everything after the final closing brace and check that braces balance. The validator shows the line and column of the first extra character.
If a program produces the file, find where it writes twice: a file opened in append mode instead of write mode, a retry that writes a second response, or logging that shares stdout with the JSON output.
Put the example to work
Use your own sample, check the result, and copy or download it. The tool processes your data locally in your browser.
Locate the extra textReference documentation