JSON errors

JavaScript, from JSON.parse(), response.json() and localStorage

Unexpected end of JSON input

The parser reached the end of the text while it was still expecting more, either because there was no text at all or because it stopped partway through an object or array. An empty string is the most common culprit.

How each browser and language words it

Chrome, Edge and Node
SyntaxError: Unexpected end of JSON input
Firefox
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Safari
SyntaxError: JSON Parse error: Unexpected EOF

Paste your JSON to find the exact line

A response that stopped halfway. Press Repair to close the open brackets and see what arrived.

Skip to editor
Explorer
Open Files

No files open

0 files open

jsoneditor.io v2

{
  "orderId": "ord_8F2K1",
  "status": "shipped",
  "items": [
    { "sku": "KB-104", "name": "Mechanical keyboard", "qty": 1 },
    
JSONUTF-8
Spaces: 2

Causes and how to fix each one

1. The response body is empty

A 204 No Content, a DELETE that returns nothing or an endpoint that errors without a body will all throw this when you call response.json(). Read the body as text first and only parse it when there's something there.

JavaScript
const response = await fetch('/api/orders/42', { method: 'DELETE' })
const text = await response.text()
const data = text ? JSON.parse(text) : null

2. The JSON was cut off

A dropped connection, a response size limit or reading a file before it finished writing leaves you with half a document. Current Chrome and Node only report Unexpected end of JSON input when the text stops right after a comma, colon or opening bracket. A cut in the middle of a value shows up as Expected ',' or '}' after property value or Unterminated string instead, so those messages can mean truncation too. Repair can close the open brackets so you can see what arrived, though the missing data itself is gone.

3. JSON.parse on an empty string from storage or a form

localStorage.getItem returns null for a missing key, which JSON.parse turns into null without complaint, but an empty string stored earlier or an empty input value throws this error. Guard for empty values before parsing.

JavaScript
const saved = localStorage.getItem('settings')
const settings = saved ? JSON.parse(saved) : {}

Questions people ask

Why does response.json() fail on a 204?

A 204 means the server deliberately sent no body. response.json() still tries to parse the empty body, and an empty string is not valid JSON, so check for a 204 or read the text first.

Can I recover the missing part of truncated JSON?

No, the data that never arrived is gone. Repairing closes the brackets so the part you did receive becomes valid JSON, which is useful for debugging, then fix whatever cut the response short.

Tools for this