Troubleshooting Knowledge Base

How to Fix JSON SyntaxError: Unexpected End of Input

Fix "SyntaxError: Unexpected end of JSON input" errors caused by unclosed brackets, truncated responses, empty strings, and incomplete API payloads.

"SyntaxError: Unexpected end of JSON input" means the JSON parser reached the end of the text before the structure was complete. The most common causes are unclosed { or [ brackets, a truncated API response, or passing an empty string to JSON.parse().

What Causes "Unexpected End of JSON Input"?

This error occurs when JSON.parse() or another JSON parser reaches the end of the input text before the document structure is complete. The JSON is incomplete or truncated.

Common Causes:

  1. Unclosed bracket: {"name": "Alice" is missing the closing }.
  2. Truncated HTTP response: A network timeout cut the API response before it finished transmitting.
  3. Empty string input: Calling JSON.parse("") or JSON.parse(null) throws this error.
  4. Partial file read: Only part of a JSON file was read before parsing began.
  5. Premature stream close: A server-sent event or WebSocket stream closed mid-JSON.
json
// Causes of "Unexpected End of JSON Input"

JSON.parse("")           // ❌ Empty string
JSON.parse('{"name"')   // ❌ Truncated — missing value and closing }
JSON.parse('[1, 2, 3')  // ❌ Missing closing ]

// Safe approach: always validate length and content
function safeParse(text) {
  if (!text || text.trim() === '') return null;
  try {
    return JSON.parse(text);
  } catch (e) {
    console.error('JSON parse error:', e.message);
    return null;
  }
}

// Check response before parsing in fetch()
const res = await fetch('/api/data');
const text = await res.text();
if (text.length === 0) throw new Error('Empty response from API');
const data = JSON.parse(text);

Try the free JSON Validator

Runs entirely in your browser — no upload, no signup, and your data never leaves your device.

Open JSON Validator

Frequently Asked Questions

What does "unexpected end of JSON input" mean?

It means the JSON parser reached the end of the input string before the JSON structure was complete — usually caused by a missing closing bracket, brace, or quote.

How do I fix a truncated JSON response?

Check your server logs for timeout errors. Add response length validation before calling JSON.parse. Use our JSON Validator to identify exactly where the structure is incomplete.

Can JSON.parse() throw on an empty string?

Yes. JSON.parse("") throws SyntaxError: Unexpected end of JSON input. Always check that the string is non-empty before parsing.

Related JSON Topics & Reference Articles