Error Encyclopedia

JSON Unexpected End of Input

Learn why 'Unexpected end of JSON input' occurs and how to fix truncated or incomplete JSON data.

What Does This Error Mean?

The 'Unexpected end of JSON input' error means the JSON parser reached the end of the input string before finding a complete, valid JSON structure. The input is truncated or incomplete.

Common Causes

1

Truncated API response due to network timeout or server error

2

Empty response body being parsed as JSON

3

Missing closing brackets or braces

4

File read operation that didn't load the complete file

5

Stream that hasn't fully finished writing

How to Fix It

Check for empty responses

Before parsing, verify the response is not empty and is the expected content type.

fetch(url)
  .then(res => {
    if (!res.ok) throw new Error(`HTTP ${res.status}`)
    return res.text()
  })
  .then(text => {
    if (!text) throw new Error("Empty response")
    return JSON.parse(text)
  })

Add error boundaries

Wrap JSON.parse in try-catch to handle malformed data gracefully.

try {
  const data = JSON.parse(response)
} catch (e) {
  console.error("Invalid JSON response:", e.message)
  // Use fallback or retry logic
}

Fix missing brackets

Ensure all objects and arrays have matching closing brackets. Use a JSON linter to find mismatched braces.

// ❌ Missing closing brace
{ "key": "value"

// ✅ Complete
{ "key": "value" }

Related Tools

Use these tools to debug and fix this error:

Related Guides

Deepen your understanding with these guides and tutorials:

Related Errors

Other common errors in this category:

Frequently Asked Questions

What is 'JSON.parse: unexpected end of data'?

It's the same as 'Unexpected end of JSON input' — just a different wording from older JavaScript engines. Both mean the JSON string was incomplete.

How do I prevent this error in production?

Always validate server responses before parsing, implement retry logic for network requests, and use TypeScript with runtime validation (like Zod or io-ts) for critical data.