Error Encyclopedia

JSON Unexpected Token

Learn what causes the 'JSON Parse error: Unexpected token' error and how to fix it in JavaScript, Python, and other languages.

What Does This Error Mean?

The 'Unexpected token' error occurs when JSON.parse() or equivalent encounters a character it does not expect at the current position in the input string. This typically means the JSON is malformed — it contains invalid syntax that does not conform to the JSON specification.

Common Causes

1

Trailing comma after the last element in an object or array

2

Single quotes used instead of double quotes for strings

3

Missing closing brace, bracket, or quote

4

BOM (Byte Order Mark) character at the start of the file

5

Comments embedded in JSON (not allowed per spec)

6

Control characters or invisible Unicode in string values

How to Fix It

Validate your JSON

Paste your JSON into a JSON validator to find the exact syntax error location. Most validators highlight the problematic character and line number.

// Use a JSON validator
JSON.parse(yourString); // throws SyntaxError: Unexpected token

Remove trailing commas

JSON does not allow trailing commas. Remove the comma after the last property in an object or the last element in an array.

// ❌ Invalid: trailing comma
{ "name": "John", "age": 30, }

// ✅ Valid
{ "name": "John", "age": 30 }

Use double quotes

JSON requires double quotes for all strings, including property names. Single quotes are not valid.

// ❌ Invalid: single quotes
{ 'name': 'John' }

// ✅ Valid
{ "name": "John" }

Strip BOM characters

If your JSON was edited with Windows Notepad or certain editors, it may have a BOM character. Strip it before parsing.

// Remove BOM from JSON string
function stripBOM(str) {
  if (str.charCodeAt(0) === 0xFEFF) return str.slice(1)
  return str
}

Before & After Examples

❌ Before
{ "users": [{ "id": 1 }, { "id": 2 },] }
✅ After
{ "users": [{ "id": 1 }, { "id": 2 }] }
❌ Before
{ 'name': 'John', 'age': 30 }
✅ After
{ "name": "John", "age": 30 }

Related Tools

Use these tools to debug and fix this error:

Related Guides

Deepen your understanding with these guides and tutorials:

Related Errors

Other common errors in this category:

Frequently Asked Questions

Why does my JSON work in one language but not another?

Some languages have more lenient JSON parsers. For example, JavaScript's eval() can parse single quotes and trailing commas, but JSON.parse() strictly follows the JSON specification.

Can I use comments in JSON?

No. JSON does not support comments by specification. Use JSONC (JSON with Comments) if your parser supports it, or strip comments with a preprocessing step.

What is 'Unexpected token < in JSON at position 0'?

This typically means you're trying to parse an HTML response as JSON. You likely have a server error page or a 404 HTML page being returned where you expected a JSON response.