Unexpected non-whitespace character after JSON at position N

A JSON document holds exactly one top-level value. The parser read a complete object or array, expected only whitespace after it, and found more text. The first value is fine; the question is what the extra text is: another record, a stray bracket, or output that was never meant to be JSON.

Seen as:

  • SyntaxError: Unexpected non-whitespace character after JSON at position 34 (line 2 column 1)
  • SyntaxError: Unexpected token { in JSON at position 34
  • SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data
  • json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 34)
  • invalid character '{' after top-level value

Input

Settings

History

Load from URL

Common causes

1. A JSON Lines (NDJSON) file parsed as one document

Logs, exports and streaming APIs often write one JSON object per line. Parse each line separately, or open the text in the JSON Lines formatter. If you need a single document, wrap the records in an array.

Before
{"event": "login", "user": "ada"}
{"event": "logout", "user": "ada"}
After
[
  {"event": "login", "user": "ada"},
  {"event": "logout", "user": "ada"}
]

2. One closing bracket too many

An extra } or ] closes the root early, and everything after it counts as trailing content. Remove the surplus bracket; PasteKit names it as “no open bracket to close”.

Before
{"user": {"name": "ada", "roles": ["admin"]}}}
After
{"user": {"name": "ada", "roles": ["admin"]}}

3. Server output appended after the JSON

A PHP warning, a debug print, or framework output written after the response body ends up behind the JSON. Make sure the handler writes nothing but the serialized body, and stops after it.

Before
{"ok": true}
<br />
<b>Warning</b>:  Undefined variable $user in /var/www/api.php on line 42
After
{"ok": true}

4. Two responses concatenated

Reading a socket or file in a loop and appending every chunk to the same buffer can join two complete documents, such as }{. Split them at message boundaries, or use a format with explicit framing such as JSON Lines.

Before
{"id": 1, "status": "queued"}{"id": 1, "status": "done"}
After
[{"id": 1, "status": "queued"}, {"id": 1, "status": "done"}]

Frequently asked questions

How do I parse a JSON Lines file in JavaScript?

Split on newlines and parse each non-empty line: text.split(‘\n’).filter(Boolean).map((line) => JSON.parse(line)). In Python, iterate over the file and call json.loads on each line.

Is whitespace after the JSON allowed?

Yes. Spaces, tabs, newlines and carriage returns before or after the value are ignored. Anything else, including a semicolon or a second value, is an error.

Why does Python call it "Extra data"?

Python’s json.loads reports the same condition as “Extra data” with the line and column where the extra text begins. Its raw_decode method can parse the first value and tell you where it ended.

Related