How to Fix "SyntaxError: Unexpected end of JSON input"
Troubleshoot and fix unexpected end of JSON errors in JavaScript, Node.js, and API pipelines. Learn how to handle empty responses, resolve truncated payloads, and auto-close missing brackets.
Launch JSON Repair Tool ↗
Automatically balance unclosed braces, arrays, and quotes.
Launch JSON Validator ↗
Pinpoint exactly which bracket or quote remained open.
The "SyntaxError: Unexpected end of JSON input" error occurs when a parser reaches the end of a string before finding the necessary closing brackets, quotes, or braces, or when attempting to parse an empty string or 204 No Content response with JSON.parse().
Diagnostic Overview
Why unexpected end of JSON input occurs
The "SyntaxError: Unexpected end of JSON input" error is thrown when a JSON parser expects more content before reaching the end of the string (EOF). Unlike an "unexpected token" error—which occurs when an illegal character is encountered—an "unexpected end" error means the stream ended prematurely.
In modern web applications, the two most common culprits are: (1) calling `response.json()` on an HTTP response with an empty body (e.g., HTTP 204 No Content or a blank 200 OK), and (2) parsing a truncated JSON string where network limits, LLM output token bounds, or buffer overflows cut off the trailing closing brackets `}` or `]`.
Additionally, reading an empty or uninitialized file from disk using `fs.readFileSync` and passing the empty string directly to `JSON.parse()` triggers this error every time because empty strings are invalid in JSON grammar.
By implementing proper response body checks and using automated bracket repair tools, you can eliminate this error from both development and production workflows.
Troubleshooting Steps
Step-by-step: How to troubleshoot and fix unexpected end errors
Verify response body presence
If using fetch(), ensure the server did not return an empty body or HTTP 204 No Content status before calling `res.json()`.
Inspect payload termination
If the JSON comes from an LLM stream, webhook, or large file transfer, check if the string was cut off mid-payload due to buffer limits.
Restore missing delimiters
Paste incomplete JSON into JSONGlow's JSON Repair tool to automatically balance and close unclosed brackets, braces, and strings.
Code Demonstration
Fixing Truncated Payloads with Missing Closing Braces
See how truncated API strings and missing closing delimiters are automatically balanced and resolved.
{
"status": "success",
"data": {
"items": [
{"id": 1, "name": "Item A"},
{"id": 2, "name": "Item B"}
// Missing closing ']' and '}'{
"status": "success",
"data": {
"items": [
{
"id": 1,
"name": "Item A"
},
{
"id": 2,
"name": "Item B"
}
]
}
}Root Causes
The 4 most common causes of unexpected end of JSON errors
Calling res.json() on an Empty or 204 Response
Why it happens: Calling `await res.json()` when an API endpoint returns an empty body (`""`) or HTTP status 204 No Content immediately throws an unexpected end of JSON error.
const res = await fetch('/api/endpoint');
const text = await res.text();
const data = text ? JSON.parse(text) : null;Incomplete LLM Streaming or Truncated Webhooks
Why it happens: AI language model streams (OpenAI, Claude) or webhooks exceeding size limits can be cut off before the final closing bracket is sent.
Reading an Empty or Newly Created File
Why it happens: Executing `JSON.parse(fs.readFileSync('config.json', 'utf8'))` on a newly created 0-byte file crashes because empty strings are not valid JSON.
Unclosed String Quotes in Multi-Line Payloads
Why it happens: Opening a string literal `"title": "Hello World` without a closing quotation mark makes the parser search to the end of the file.
JSONGlow Developer Kit
Tools to diagnose and repair incomplete JSON
Automated Delimiter Repair
Inspects incomplete JSON trees and automatically balances missing closing braces, quotes, and array brackets.
Line & Column Validator
Verifies structural completeness and alerts you to unclosed brackets with exact character positions.
Visual Tree Inspector
Inspect truncated objects to see exactly which nested branches were fully parsed before truncation.
JSON to TypeScript & Zod
Generate type contracts and runtime schemas from repaired JSON payloads.
100% Client-Side Privacy
All diagnostic and repair routines execute locally in your browser sandbox without network uploads.
Zero Setup & Completely Free
Instant access to all developer utilities with no registration, rate limits, or subscriptions.
Frequently Asked Questions
Common questions answered.
Need more help? Our tools execute entirely in your browser without transmitting any payload to remote servers.
Why does JavaScript throw "SyntaxError: Unexpected end of JSON input"?+
This error indicates that the parser reached the end of the data stream while still expecting more tokens. For example, if an object opened with `{` but never reached a matching `}`, or if the input string was completely empty, the parser reaches EOF unexpectedly and throws this error.
How do I safely parse API responses that might be empty?+
Read the response as text first: `const text = await response.text(); const data = text ? JSON.parse(text) : {};`. This prevents crashes on empty 200 OK or 204 No Content responses.
Can JSONGlow repair truncated JSON automatically?+
Yes! Paste your incomplete JSON into JSONGlow's JSON Repair tool. It will analyze unclosed brackets, braces, and string quotes, automatically appending matching closing tokens to make the payload parseable.
Is an empty string `""` valid JSON?+
No. The JSON specification requires a valid root value (an object `{}`, array `[]`, string `"..."`, number, boolean, or null). An empty string contains zero tokens and is therefore invalid.
Explore More Tools