February 9, 2026 IZHubs

How to fix "SyntaxError: unexpected end of JSON input" (Fetch, Node, Next.js)

Complete guide to fixing the "unexpected end of JSON input" error. Learn why this happens in fetch, Next.js, and Node.js, and copy the exact code to fix it.

What Does “Unexpected end of JSON input” Mean?

The error SyntaxError: unexpected end of JSON input is one of the most common issues developers face when processing data. But what does it actually mean?

Simply put, this error happens when the JSON.parse() engine expects more text to complete a valid JSON string, but the string suddenly ends (it hits an “End of File” or EOF). This is usually because the string is either completely empty ("" or null), truncated during a network transfer, or the server returned an HTML error page (like a 404 or 500) instead of a JSON payload.

Need to quickly find the syntax error? Paste your messy payload into our Free JSON Formatter & Validator to instantly highlight exactly where your JSON is broken or missing a closing bracket.


1. The Most Common Cause: Using fetch() Without Checking Responses

The absolute most frequent reason you see the fetch unexpected end of JSON input error is blindly calling response.json() on an HTTP request that actually failed.

If a server returns a 500 Internal Server Error, 404 Not Found, or 204 No Content, the body will either be empty or contain HTML text like <!DOCTYPE html>. When fetch tries to run .json() on that empty or HTML response, it crashes immediately.

❌ The Wrong Way

// This will crash if the server returns an empty body or a 404
const response = await fetch('https://api.example.com/data');
const data = await response.json(); 
console.log(data);

✅ The Right Way (Using response.ok)

const response = await fetch('https://api.example.com/data');

// Always check if the HTTP status is a success (200-299)
if (!response.ok) {
    const errorText = await response.text();
    throw new Error(`HTTP Error: ${response.status} - ${errorText}`);
}

// Handle empty responses (like 204 No Content)
const text = await response.text();
const data = text ? JSON.parse(text) : {}; // Parse only if text exists

console.log(data);

Next.js & React (nextjs unexpected end of json input)

In Next.js, particularly with API Routes (App Router or Pages Router), this error often occurs when you try to parse a readable stream twice, or when your component tries to parse an API response that timed out.

In React, if you load state from localStorage but the key is empty, JSON.parse('') will throw this error. Always fallback:

// React LocalStorage Fix
const savedValue = localStorage.getItem('myKey');
const parsedValue = savedValue ? JSON.parse(savedValue) : null;

Node.js & Express (nodejs unexpected end of json input)

When building a backend with Node.js and Express, you might see this error if a client sends a partial request. It frequently happens if req.body is being parsed by body-parser or express.json(), but the client did not send the correct Content-Type: application/json header, leading to an empty body parsing attempt.

Fix: Ensure middleware is properly catching JSON errors:

app.use(express.json());

// Error handling middleware for bad JSON
app.use((err, req, res, next) => {
    if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
        return res.status(400).send({ message: "Bad JSON payload." });
    }
    next();
});

Golang & GraphQL (golang unexpected end of json input)

In Go (Golang), the unexpected end of JSON input error comes from the encoding/json package when json.Unmarshal() attempts to decode an incomplete byte slice. This usually means your Go backend read from an io.Reader (like an HTTP response body) but the connection closed before the full JSON was transmitted.

For GraphQL requests, if the endpoint throws a network error, the response won’t be a valid JSON GraphQL structure, triggering the same issue on the client Apollo or URQL fetcher.


3. Fixing Npm & Yarn Build Errors

If you aren’t writing code, but you suddenly see npm run build unexpected end of json input or yarn-metadata.json unexpected end of json input in your terminal, the issue is not your code!

This happens because a package manager’s cache or .lock file (like package-lock.json or yarn.lock) got corrupted during an interrupted download.

How to Fix NPM/Yarn Errors:

Run these commands in your project terminal to clear the corrupted JSON files and reinstall:

# For NPM users
rm -rf node_modules
rm package-lock.json
npm cache clean --force
npm install

# For Yarn users
rm -rf node_modules
rm yarn.lock
yarn cache clean
yarn install

4. Why Does it Happen in VSCode or Docker?

VSCode (vscode unexpected end of json input)

If Visual Studio Code throws this error, your settings.json, launch.json, or tasks.json file is missing a closing curly brace } or bracket ]. Open the JSON file in the editor; VSCode will usually highlight the specific line where the syntax breaks.

Docker (docker unexpected end of json input)

When reading Docker container logs or configurations, the Docker daemon outputs JSON. If the container crashes violently (Out of Memory/OOM Kill), the stream abruptly ends, causing your logging aggregators to throw an unexpected end of input.


Frequently Asked Questions (FAQ)

What does “unexpected end of json input at json parse anonymous” mean?

This indicates that the JSON.parse() method was called inside an anonymous function (like an inline arrow function .then(res => res.json())) and the input provided to it was empty or invalid.

What is “failed to execute json on response unexpected end of json input”?

This is specifically the browser (like Chrome or Firefox) telling you that the native Response.json() method of the Fetch API failed because the network stream closed before the server finished sending the full JSON text.

How do I fix “unexpected end of json input” on Blooket or Civitai?

If you are a user trying to access game platforms like Blooket or AI hubs like Civitai and see this error, it means their servers are currently overloaded and returning empty error pages instead of game/model data. You cannot fix this with code; you must refresh the page, clear your browser cache, or wait for their servers to recover.


Still stuck? The best debugging step is to visually inspect the data you are trying to parse. Use a JSON Formatter & Validator to paste your raw data and instantly find the syntax break.