jsonerrorsdebuggingapijavascript

Unexpected End of JSON Input — Causes and Fixes

·5 min read

What Is "Unexpected End of JSON Input"?

This error from JSON.parse() means the string ended before the JSON was complete. The parser was in the middle of reading an object, array, or string and ran out of characters.

SyntaxError: Unexpected end of JSON input

Common Causes

### Cause 1: Empty Response Body

The most common cause. Your server or API returned an empty body — a 204 No Content, or a 200 OK with nothing in the body. Calling .json() on an empty body produces this error.

javascript
// This will throw if the body is empty
const data = await response.json();

Fix — check for empty body first:

javascript
const text = await response.text();
const data = text ? JSON.parse(text) : null;

### Cause 2: Truncated JSON

The response was cut off mid-transfer. This can happen with network timeouts, server crashes during streaming, or a proxy that truncates large responses.

Bad (truncated):

json
{"users": [{"name": "Ravi", "age": 28}, {"name": "Priy

The parser reaches the end of input while still inside a string and throws.

### Cause 3: Parsing an Undefined or Null Variable

javascript
const raw = localStorage.getItem('data'); // returns null if key doesn't exist
const data = JSON.parse(raw); // throws: null is not valid JSON

Fix — guard before parsing:

javascript
const raw = localStorage.getItem('data');
const data = raw ? JSON.parse(raw) : defaultValue;

### Cause 4: Empty String

javascript
JSON.parse(''); // SyntaxError: Unexpected end of JSON input
JSON.parse('   '); // same

An empty string is not valid JSON. The smallest valid JSON values are `null`, `0`, `""`, `true`, `[]`, and `{}`.

### Cause 5: Incomplete JSON Written to a File

If a write operation was interrupted — server crash, disk full, process killed — a JSON file on disk may be incomplete. The file ends in the middle of a structure.

Check by reading the last few characters of the file. A complete JSON object ends with `}`, a complete array ends with `]`.

Diagnostic Pattern

Use this wrapper to log exactly what was received when parsing fails:

javascript
function safeJSON(str, fallback = null) {
  if (!str || !str.trim()) return fallback;
  try {
    return JSON.parse(str);
  } catch (e) {
    console.error('JSON parse error:', e.message);
    console.error('Input was:', JSON.stringify(str.slice(0, 200)));
    return fallback;
  }
}

Fix for API Responses

javascript
async function fetchJSON(url) {
  const response = await fetch(url);

  // 204 No Content — no body expected
  if (response.status === 204) return null;

  const text = await response.text();

  if (!text.trim()) {
    console.warn('Empty response body from', url);
    return null;
  }

  return JSON.parse(text);
}

Fix for localStorage

javascript
function loadFromStorage(key, fallback) {
  try {
    const raw = localStorage.getItem(key);
    return raw !== null ? JSON.parse(raw) : fallback;
  } catch {
    localStorage.removeItem(key); // clear corrupted data
    return fallback;
  }
}

Validate Before Saving

If you write JSON to files or databases, validate it first. Paste your JSON into JSONKit's validator at /json-validator to confirm it is complete and valid before saving.

Try JSON Validator

Find the missing bracket, brace or quote causing the truncation error.