Fix "SyntaxError: Unexpected token in JSON at position"

April 2026 · 7 min read

If you're seeing SyntaxError: Unexpected token X in JSON at position N in your browser console or Node.js app, your JSON has a syntax error. This guide covers every common cause and how to fix each one in under a minute.

Paste your JSON here to find the error →

The 7 Most Common Causes

1. Trailing commas

JSON does not allow a comma after the last item in an array or object. JavaScript does; JSON doesn't.

// WRONG — trailing comma after "banana"
{"fruits": ["apple", "banana",]}

// CORRECT
{"fruits": ["apple", "banana"]}

2. Single quotes instead of double quotes

JSON requires double quotes for all strings and keys. Single quotes are not valid JSON.

// WRONG
{'name': 'Alice'}

// CORRECT
{"name": "Alice"}

3. Unquoted keys

Unlike JavaScript objects, JSON keys must always be quoted.

// WRONG
{name: "Alice"}

// CORRECT
{"name": "Alice"}

4. Comments in JSON

Standard JSON does not support comments. Remove all // and /* */ before parsing.

// WRONG
{
  "debug": true // enable debug mode
}

// CORRECT
{
  "debug": true
}

5. Missing or extra brackets

Every { needs a matching }. Every [ needs a ]. A missing bracket is the most common cause of "unexpected end of JSON input."

6. HTML or XML in your JSON response

If your API returns an error page (HTML) instead of JSON, JSON.parse() will fail on the first < character. Check the HTTP status code before parsing — if it's 4xx or 5xx, the response is probably an error page, not JSON.

// Always check before parsing
const response = await fetch('/api/data');
if (!response.ok) {
  throw new Error('HTTP ' + response.status);
}
const data = await response.json();

7. BOM (Byte Order Mark) or invisible characters

Some text editors add invisible characters at the start of files. If your JSON file starts with \uFEFF (BOM), strip it before parsing:

const clean = text.replace(/^\uFEFF/, '');
const data = JSON.parse(clean);

How to Find the Exact Error Position

The error message includes a position number (e.g., "at position 42"). This is the character index where parsing failed. To find it quickly:

1. Open the ToolPry JSON Formatter

2. Paste your JSON

3. The formatter shows the exact error with a description

4. Fix the error and re-format

The formatter runs entirely in your browser — your data never leaves your machine, so it's safe for API keys, tokens, and sensitive config files.

Quick Fixes by Error Message

Unexpected token ' at position 0 — You're using single quotes. Replace all ' with ".

Unexpected token < at position 0 — Your API returned HTML (probably an error page). Check the HTTP status code and Content-Type header.

Unexpected token } at position N — You have a trailing comma before the closing brace. Remove it.

Unexpected end of JSON input — Your JSON is truncated or has a missing closing bracket. Check that all { and [ are closed.

Unexpected token u at position 0 — You're trying to parse the string undefined. Check that your variable actually contains JSON data before calling JSON.parse().

Fix your JSON now →

Related Tools

JSON Formatter & Validator — format, validate, and fix JSON errors instantly.

Base64 Encoder — decode Base64-encoded JSON payloads.

URL Encoder — decode URL-encoded JSON in query strings.

Regex Tester — test patterns for extracting data from malformed JSON.