How to Fix 'SyntaxError: Unexpected end of JSON input'
Why This Error Happens
This error occurs when JSON.parse() reaches the end of the input string while still expecting more characters — typically due to empty responses (0 bytes), unclosed curly braces '{', unclosed brackets '[', or an unclosed string quote '"'.
The JSON parser parses tokens sequentially. If the payload cuts off before all open braces ({), square brackets ([), or quotation marks (") are closed, the parser hits End-Of-File (EOF) unexpectedly.
{
"userId": 101,
"username": "developer",
"roles": ["admin", "editor"
{
"userId": 101,
"username": "developer",
"roles": ["admin", "editor"]
}Step-by-Step Fix
- 1Ensure the JSON string is not completely empty (length > 0) before calling JSON.parse().
- 2Verify that every opened '{' and '[' has a matching '}' and ']'.
- 3Ensure strings spanning multiple lines do not have premature breaks or missing closing quotes.
- 4Use our JSON Validator with 'Fix with AI' to automatically close missing brackets and quotes.
Test and Auto-Fix Broken JSON Online
Paste your raw payload into our free, client-side JSON Validator. Detect syntax errors with line numbers and fix them in one click.
Open Free JSON ValidatorFrequently Asked Questions
Why does an empty response cause Unexpected end of JSON input?
An empty string '' is not valid JSON according to RFC 8259. A valid JSON payload must be at least a valid literal (e.g., null, {}, or []). Calling JSON.parse('') immediately throws this error.
How do I handle empty responses in fetch?
Check the content-length or use const text = await response.text(); return text ? JSON.parse(text) : null;.