AllJSONTools

Free JSON Developer Tools

Fix "Unexpected End of JSON Input" — Causes, Examples & Solutions

2026-03-19 · 8 min read · By AllJSONTools

JSON Errors
Debugging
JavaScript
Having JSON issues?

Paste broken JSON and fix it instantly with AI — plain-English explanations included.

Fix JSON with AI

What Is “Unexpected End of JSON Input”?

If you have ever called JSON.parse() in JavaScript and received the error SyntaxError: Unexpected end of JSON input, you know how frustrating it can be. The error message tells you almost nothing about what went wrong or where the problem is. All it means is that the JSON parser reached the end of the string before it found all the closing brackets, braces, or quotes it was expecting. In other words, your JSON is incomplete or truncated.

This error is one of the most common JSON parsing failures in web development. It shows up when fetching API responses, reading files, processing webhooks, and parsing user input. The good news is that the causes are predictable and the fixes are straightforward. If you want an instant fix, paste your broken JSON into our AI-powered JSON fixer tool — it detects and repairs these issues automatically.

What Causes This Error?

Every valid JSON document must be a complete, self-contained structure. Objects must have matching opening and closing braces. Arrays must have matching brackets. Strings must have opening and closing double quotes. When JSON.parse() starts reading your input, it builds an internal expectation of what should come next. If it reaches the end of the input while still expecting a closing character, it throws “Unexpected end of JSON input.”

The root cause always boils down to one thing: the JSON string is not complete. This can happen because you accidentally deleted a closing brace, because an API response was truncated by a network timeout, because you are trying to parse an empty string, or because the server returned HTML instead of JSON. The parser does not care why the data is incomplete — it only knows that it expected more characters and found none.

Unlike the closely related “Unexpected token” error (which means the parser found an invalid character), “Unexpected end of JSON input” specifically means the parser ran out of data. This distinction is helpful for debugging because it narrows the problem to missing or truncated content rather than malformed syntax.

6 Common Causes of “Unexpected End of JSON Input”

Below are the six most common scenarios that trigger this error. Each one includes the broken JSON, an explanation of what went wrong, and the corrected version.

1. Missing Closing Brace

The most straightforward cause: an object was opened with { but never closed with }. The parser reads through all the key-value pairs, reaches the end of the string, and realizes the object was never terminated.

Broken

json
{"name": "Alice", "age": 30

Fixed

json
{"name": "Alice", "age": 30}

This often happens when manually editing JSON or when code generates JSON by concatenating strings instead of using JSON.stringify(). Always use a proper serializer to avoid this class of error entirely.

2. Missing Closing Bracket

Arrays need a closing ] to match their opening [. A common variant of this bug is closing an array with } instead of ], or simply forgetting the bracket entirely.

Broken

json
{"items": [1, 2, 3}

Fixed

json
{"items": [1, 2, 3]}

In deeply nested JSON, bracket mismatches can be hard to spot visually. Use the JSON Formatter to auto-indent and highlight mismatched brackets, or paste your JSON into the JSON Validator to see the exact error position.

3. Unclosed String Value

When a string value is missing its closing double quote, the parser keeps reading everything that follows as part of the string — including commas, colons, braces, and other values. Eventually it reaches the end of the input without ever finding the closing quote.

Broken

json
{"name": "Alice, "age": 30}

Fixed

json
{"name": "Alice", "age": 30}

The missing closing quote after “Alice” causes the parser to interpret Alice, "age": 30} as a single, unterminated string. This is particularly tricky to debug because the error position reported by the parser is at the very end of the document, far from the actual missing quote.

4. Truncated API Response

Network timeouts, proxy buffer limits, and body size restrictions can all cut an API response mid-stream. The resulting JSON string is incomplete and will fail to parse.

Broken (truncated mid-response)

json
{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "na

Fixed (complete response)

json
{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  ]
}

To detect truncation, compare the Content-Length header with the actual number of bytes received. If they do not match, the response was truncated. Consider increasing your timeout settings, using streaming JSON parsers for large payloads, or implementing pagination to reduce response sizes.

5. Empty Response Body

Calling JSON.parse("") on an empty string immediately throws “Unexpected end of JSON input.” This is one of the most common triggers, especially when an API returns a 204 No Content status or returns an empty body on error responses.

Broken

javascript
const response = await fetch("/api/data");
const data = await response.json();
// Throws if the body is empty!

Fixed

javascript
const response = await fetch("/api/data");

if (!response.ok) {
  throw new Error(`HTTP error: ${response.status}`);
}

const text = await response.text();
if (!text) {
  // Handle empty response gracefully
  return null;
}

const data = JSON.parse(text);

Always check response.ok and verify the body is not empty before attempting to parse. Reading the body as text first gives you the opportunity to inspect what the server actually returned before parsing fails.

6. Parsing HTML Instead of JSON

When a server returns an HTML error page (like a 404 or 502) instead of a JSON response, calling response.json() will fail. The parser encounters < as the first character and throws either “Unexpected token” or “Unexpected end of JSON input” depending on the parser implementation.

What the server returned

html
<!DOCTYPE html>
<html>
<body>404 Not Found</body>
</html>

How to handle it

javascript
const response = await fetch("/api/data");
const contentType = response.headers.get("content-type");

if (!contentType || !contentType.includes("application/json")) {
  const text = await response.text();
  throw new Error(`Expected JSON, got: ${text.substring(0, 100)}`);
}

const data = await response.json();

Always check the Content-Type header before parsing. Verify that the URL is correct and that you are hitting the API endpoint, not a web page. This is especially common when a reverse proxy or CDN serves a fallback HTML page for missing routes.

If you have broken JSON from any of these scenarios, paste it into our AI-powered JSON fixer tool — it detects and repairs these issues automatically.

How to Fix “Unexpected End of JSON Input” — Step by Step

When you encounter this error, follow this systematic approach to identify and resolve the issue quickly.

  1. Step 1: Check if the response is actually JSON. Before parsing, verify the Content-Type header. If it says text/html instead of application/json, the problem is not with your JSON — it is with the server response. Log the raw response body to see what you actually received.

  2. Step 2: Validate the JSON string. Paste the raw JSON into the JSON Validator. It will tell you exactly where parsing fails and what the parser expected at that position.

  3. Step 3: Count your brackets. Every { needs a matching }, every [ needs a matching ], and every " needs a closing ". If the counts do not match, you have found your problem.

  4. Step 4: Check for truncation. Compare the Content-Length header with the actual body length. If the body is shorter than expected, the response was truncated in transit. Check your proxy, CDN, or server timeout settings.

  5. Step 5: Use a JSON fixer tool. If you cannot find the issue manually, paste your JSON into the JSON Fixer. It uses AI to detect missing brackets, unclosed strings, and other structural problems, and repairs them automatically.

Best Practices to Avoid This Error

Prevention is better than debugging. Follow these practices to stop “Unexpected end of JSON input” from appearing in the first place.

  1. Always check response.ok before parsing. If the HTTP status indicates an error, the body may be empty or may contain HTML. Handle error responses separately from success responses.

  2. Wrap JSON.parse() in a try/catch. Never let a parse error crash your application. Catch the error, log the raw input for debugging, and return a meaningful error message to the user.

  3. Validate the Content-Type header. Before calling response.json(), confirm that the server returned application/json. This one check prevents most HTML-instead-of-JSON errors.

  4. Set appropriate timeouts for API calls. Short timeouts cause responses to be truncated. Set timeouts that are long enough for your largest expected response, and implement retry logic for transient network failures.

  5. Use streaming JSON parsers for large payloads. If your API returns megabytes of JSON, consider using a streaming parser that processes data incrementally rather than buffering the entire response into memory before parsing.

  6. Validate JSON before storing in databases. If you accept JSON input from users or external systems, validate it at the boundary before storing it. This prevents corrupt data from propagating through your system.

Tools to Fix and Prevent JSON Errors

These free tools can help you diagnose and repair JSON errors instantly:

  • Fix JSON Error — AI-powered tool that automatically detects and repairs broken JSON, including missing brackets, unclosed strings, and truncated data.

  • JSON Validator — Validates your JSON and shows the exact line and character position where parsing fails, making it easy to locate the problem.

  • JSON Formatter — Prettifies and auto-indents JSON so you can visually spot missing brackets, mismatched braces, and structural problems.

Frequently Asked Questions

What does “Unexpected end of JSON input” mean?

It means the JSON parser reached the end of the input string before finding all the closing characters it expected. The JSON is incomplete — it is missing one or more closing braces, brackets, or quotes. This can also occur when trying to parse an empty string.

Why does fetch() throw “Unexpected end of JSON input”?

The fetch() API itself does not throw this error. The error comes from calling response.json() on a response that has an empty body, a non-JSON body (like HTML), or a truncated JSON body. Always check response.ok and the Content-Type header before parsing.

How do I fix “Unexpected end of JSON input” in JavaScript?

First, log the raw string you are trying to parse to see what it actually contains. If it is empty, the issue is with the data source, not the parsing. If it is truncated JSON, check for missing closing braces or brackets and add them. If you are unsure what is wrong, paste the string into the JSON Fixer for automatic repair.

Can I fix truncated JSON automatically?

In many cases, yes. If the JSON is only missing closing brackets and braces, a fixer tool can analyze the nesting structure and add the missing terminators. However, if content was cut mid-value (like a string or number being truncated), the lost data cannot be recovered. The best approach is to fix the upstream issue causing the truncation and re-fetch the data.

What is the difference between “Unexpected token” and “Unexpected end of JSON input”?

“Unexpected token” means the parser found a character that is not allowed at that position — like a single quote, a comment, or undefined. “Unexpected end of JSON input” means the parser ran out of characters before the document was complete. Both are SyntaxError types, but they point to different root causes. For a comprehensive list of JSON syntax errors and their fixes, see our guide on fixing common JSON errors.

How do I prevent this error in production?

Implement defensive parsing: always wrap JSON.parse() in a try/catch block, validate Content-Type headers, check for empty bodies before parsing, set adequate request timeouts, and log the raw response body when parsing fails so you can diagnose the issue. These five practices will eliminate the vast majority of unexpected JSON parsing failures in production.

Having JSON issues?

Paste broken JSON and fix it instantly with AI — plain-English explanations included.

Fix JSON with AI