How to Fix "Unexpected Non-Whitespace Character After JSON" Errors
This error is actually good news, in a strange way — it means JSON.parse() successfully found and parsed a complete, valid JSON value, but then found additional non-whitespace content after it that shouldn't be there. JSON.parse() expects the entire input string to be exactly one JSON value, nothing more, so anything trailing after that value is treated as an error rather than silently ignored.
The most common cause is concatenated JSON — two or more separate JSON objects or arrays stuck together in a single string with no separator, often from log files or streaming data sources where each line or chunk is its own valid JSON document, but the whole file or stream isn't itself one valid JSON document when read as a single string.
A closely related cause is JSON Lines format (sometimes called NDJSON), a deliberate format where each line of a file is its own independent JSON object, and the file as a whole is never meant to be parsed as one JSON document — if code written for regular JSON accidentally receives a JSON Lines file and tries to JSON.parse() the whole thing at once, this error is the direct result.
Trailing garbage characters are a simpler, more mundane cause — an extra character accidentally included at the end of a JSON string, commonly a stray comma, a trailing semicolon left over from surrounding code, or invisible whitespace-adjacent characters that got copy-pasted along with the real content and aren't being displayed by whatever editor was used to prepare the string.
To fix the concatenated or JSON-Lines case, the correct approach is not trying to parse the whole string as one call to JSON.parse() at all — instead, split the input into its individual JSON documents first (by newline, for JSON Lines, or with a streaming JSON parser for more complex concatenation patterns) and parse each one separately.
For the simpler trailing-garbage case, trimming the string and checking its exact final characters before parsing usually reveals the culprit quickly — our JSON Validator will point out precisely where valid JSON ends and unexpected content begins, which is much faster than manually scanning a long string character by character looking for something that doesn't belong.
Found this helpful?
SyncTonight's tools and guides are free and always will be. If this post saved you some debugging time, a coffee goes a long way — no pressure, just appreciated.
☕ Buy me a coffee