All posts
javascriptjsondebugging

JSON.parse unexpected token — What It Means and How to Fix It

"JSON.parse unexpected token" explained — why it happens, a real code example that triggers it, and the exact fix.

SR

Suhail Roushan

August 6, 2026

·
3 min read
·
0 views

"JSON.parse unexpected token" is a JavaScript syntax error thrown when the parser hits a character it doesn't recognize while reading a string as JSON.

What "JSON.parse unexpected token" Means

The JSON.parse() method expects a valid JSON string. When it encounters a character that isn't part of the JSON specification — like a trailing comma, an unquoted key, or a stray quote — it throws SyntaxError: Unexpected token. This is a runtime error, not a compile-time one, meaning it fires in the browser console or Node.js terminal at the moment your code executes.

Why It Happens

The two most common real causes:

  1. Malformed JSON from an API response — A server returns HTML, a 404 page, or a truncated payload instead of JSON. Your code assumes JSON, so the parser chokes on the first < or { it doesn't expect.
  2. Trailing commas or single quotes in hand-written JSON — JSON is strict. {"name": 'Suhail',} fails because single quotes aren't valid and trailing commas are forbidden. JavaScript object literals allow both; JSON does not.

Example Code That Triggers It

Here's a minimal, runnable example that produces the exact error in Node.js or the browser:

const brokenJson = '{"name": "Suhail", "role": "developer",}'; // trailing comma
try {
  const data = JSON.parse(brokenJson);
  console.log(data);
} catch (err) {
  console.error(err.message); // "Unexpected token } in JSON at position 43"
}

Run this in any modern browser DevTools console or with node index.js. The parser reaches the } after the trailing comma and throws immediately.

How to Fix It

Remove the trailing comma and ensure strict JSON syntax:

const validJson = '{"name": "Suhail", "role": "developer"}';
const data = JSON.parse(validJson);
console.log(data.name); // "Suhail"

The fix works because JSON.parse only accepts strings that conform to the ECMA-404 standard. No comments, no trailing commas, no single quotes. If you're fetching from an API, validate the response before parsing:

const res = await fetch('/api/user');
const text = await res.text();
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = JSON.parse(text); // now safe

Common Mistakes That Cause This

Mistake 1: Parsing an empty string. JSON.parse("") throws Unexpected end of JSON input, but an empty response body from a server often surfaces as Unexpected token if the response is whitespace. Always check text.trim().length > 0 before parsing.

Mistake 2: Copy-pasting JSON from browser console logs. The console displays objects with single quotes or undefined values that aren't valid JSON. What you see is a JavaScript representation, not the raw string. Use JSON.stringify() to get the real serialized form.

When Should You Worry About This?

You should worry when this error appears in production logs repeatedly. A single occurrence is likely a transient network issue. Consistent failures mean your API contract is broken — the server is sending non-JSON content, or your client is sending malformed request bodies. If you're using Next.js API routes, check that you're not returning undefined from a handler, which serializes to an empty string. For TypeScript projects, this error is purely runtime — the compiler won't catch it, so wrap JSON.parse calls in a try/catch or use a validation library like Zod to fail gracefully.

Next time this error appears, check the exact position in the error message (e.g., "at position 43") and inspect that character in your raw string first — it's almost always a trailing comma or a stray quote.

Related posts

Written by Suhail Roushan — Full-stack developer. More posts on AI, Next.js, and building products at suhailroushan.com/blog.

Get in touch