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.

  • SyntaxError: Unexpected non-whitespace character after JSON at position 26 (line 2 column 1)
  • SyntaxError: Unexpected token { in JSON at position 26
  • SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data
  • SyntaxError: JSON Parse error: Unable to parse JSON string

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.

Fails to parse
{"id": 1, "status": "ok"}
{"id": 2, "status": "ok"}
Parses
[
  {"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.

Your JSON and text are processed entirely in your browser and are never sent anywhere. To understand how the site is used we record each page view (the page, the referrer and an approximate location from your IP address) and keep it for 90 days. Nothing is stored on your device unless you choose it. Accept to also store a device identifier here and record your browser and device characteristics, which is what lets a return visit be told from a new one. Decline and nothing is sent at all. We respect Do Not Track. See our privacy policy.