JSON.parse: Unexpected End of JSON Input: How to Fix It

If you got this error:

SyntaxError: Unexpected end of JSON input

the message already tells you a lot: the parser reached the end of the string before it finished interpreting the JSON. In other words, something is missing. Usually a }, a ], a closing quote, or the string is empty altogether.

Unlike “unexpected token” errors, which point to a wrong character somewhere in the middle, this one is about something that should be there and isn’t.

The most common causes

1. Empty JSON string

// javascript
JSON.parse(""); // Unexpected end of JSON input

This is pretty common when you’re expecting data from an API or a field, but the value comes back empty. For example, a network response that failed silently, or a form field that wasn’t filled in.

How to fix it: validate before parsing.

// javascript
if (text && text.trim() !== "") {
  const data = JSON.parse(text);
}

2. Unclosed brace or bracket

// javascript
JSON.parse('{"name": "test"'); // missing closing }
// javascript
JSON.parse('["item1", "item2"'); // missing closing ]

This happens a lot when the JSON is built by hand, or when an API response comes back truncated (for example, if the request was cut off midway by a timeout or a size limit).

3. Unclosed string quotes

// javascript
JSON.parse('{"name": "test}'); // missing the closing quote after test

Easy to run into when the JSON has special characters inside a string and one of them ends up escaping a quote unintentionally, closing it too early, or when a quote that should be inside the value isn’t escaped properly.

4. API response cut off midway (truncated)

This one deserves its own attention. If you’re consuming an API and the JSON comes back truncated, the error you get is exactly this one: unexpected end of JSON input, because the parser reaches the end of the received string without every {, [, or open quote having been closed.

Common causes of this:

  • A request timing out before the full response arrives
  • A response size limit set on the server side, or on a proxy/CDN somewhere in between
  • A data stream that got interrupted (common with large responses read in chunks)

How to debug it: log the length and the raw content of the response before parsing, to confirm it actually arrived complete.

// javascript
fetch('/api/data')
  .then(res => res.text())
  .then(text => {
    console.log('Response length:', text.length);
    console.log('Last 50 characters:', text.slice(-50));
    const data = JSON.parse(text);
  });

If the last characters don’t form a valid closing (like ending in the middle of a key, with no final }), the response was cut off before it reached you. In that case, the problem isn’t in your parsing code, it’s in the source of the response.

Quick checklist

  • Is the string you’re parsing not empty?
  • Does every { have a matching }?
  • Does every [ have a matching ]?
  • Are all string quotes properly closed?
  • If it’s an API response, did it arrive complete (not truncated by a timeout or size limit)?

To quickly check if a JSON is balanced (all braces and brackets properly closed), paste it into JSON Validator and see the result instantly.