Code Beautifier
Unexpected end of JSON input

Fix "Unexpected end of JSON input"

What "Unexpected end of JSON input" means, the four situations that cause it (truncated data, an empty string, a missing bracket), and how to fix each.

Input that triggers it

{
  "user": {
    "id": 42,
    "tags": ["beta", "internal"
  }
Open JSON Formatter on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Indent:
json
json

What the error means

Unexpected end of JSON input means the parser ran out of text while the document was still open. Every { needs a } and every [ needs a ]; when the input ends with some of them still unmatched, there is nothing left to read and the parser gives up. Unlike most JSON errors, this one usually has no useful position: the problem is at the very end, where there is nothing to point at.

The example above is missing two characters — a ] after "internal" and a final } — which is enough to make the whole document unparseable.

Why it happens

  • Truncated data. A response body cut off by a timeout, a proxy limit, a log line that was clipped, or a copy-paste that stopped one line short. This is the most common cause in real systems.
  • An empty string. JSON.parse("") throws exactly this message. It happens when a fetch resolved with no body, a file was created but never written, or a variable was never assigned. The document did not end early — it never started.
  • A missing closing bracket in a hand-edited file, typically after deleting the last item of an array.
  • Reading a file before it is fully written, so the parser sees a partial document.

How to fix it

  1. Paste the text into the formatter above. If the editor shows the document, count the opening and closing brackets; the formatter's error card tells you the parse stopped at the end.
  2. Work from the end backwards. Each unmatched { or [ needs its closer added in reverse order of opening. For the example, that is ] then } then }.
  3. If the text is genuinely empty or ends mid-value (say "tags": ["be), no amount of bracket-adding will recover the lost content. Fix the source: increase the timeout, check the response size, or read the file after the write completes.
  4. If you are parsing an HTTP response, log response.status and the raw body first. An empty body with a 204 or a 500 status is not a JSON problem.

A correct version of the example:

{
  "user": {
    "id": 42,
    "tags": ["beta", "internal"]
  }
}

JSON Repair will close unmatched brackets for you when the structure is obvious, which makes it the fastest route for a document you only need to read rather than ship.

If it still fails

  • Run the same text through JSON Validator to see the structure it did manage to read before the end.
  • If a value ends mid-string, the text is truncated, not malformed; recover it at the source.
  • Watch for a BOM or invisible character at the very end of a file that a text editor hides; the parser will report the end of input rather than the character.

Related errors