Home/Guides/Unexpected End of JSON
SyntaxError Troubleshooting Guide

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.

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

1. Check for Empty Response01

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()`.

2. Check for Truncated JSON02

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.

3. Auto-Close with JSON Repair03

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.

Incomplete JSON (Throws Unexpected End of JSON)
Input
{
  "status": "success",
  "data": {
    "items": [
      {"id": 1, "name": "Item A"},
      {"id": 2, "name": "Item B"}
    
// Missing closing ']' and '}'
Repaired JSON (Balanced & Valid RFC 8259)
Processed
{
  "status": "success",
  "data": {
    "items": [
      {
        "id": 1,
        "name": "Item A"
      },
      {
        "id": 2,
        "name": "Item B"
      }
    ]
  }
}
Key Difference:The repair engine detects unclosed structures and automatically appends the necessary closing brackets `]` and braces `}` to produce valid, parseable JSON.

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.

Solution:Inspect response headers and status before parsing, or parse as text first.
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.

Solution:Buffer the full stream before parsing, or pass partial chunks through JSON Repair to balance unclosed brackets.
!

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.

Solution:Provide a fallback default: `JSON.parse(content || '{}')`.
!

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.

Solution:Close all string quotations properly before parsing.

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

The JSONGlow developer kit.