Quick answer

The JSON "unexpected token" error means the parser encountered a character it did not expect at that position, often due to a comma, quote, or bracket issue.

JSON Unexpected Token Error

Unexpected token is one of the most common JSON errors. It usually means the parser hit a character or symbol it did not expect at the current position.

Common causes

How to fix

Examples

Bad

{"a": 1, "b": 2,}

Good

{"a": 1, "b": 2}

JSON unexpected token errors happen when a parser encounters a character, symbol, or structure that does not match valid JSON syntax at the current position. This is one of the most common issues developers see when working with APIs, configuration files, request payloads, and data exports. It often points to a formatting problem such as a missing quote, trailing comma, unescaped character, or malformed value. This page explains what the error means, how to identify the likely cause, and how to correct it safely so your JSON can be parsed by browsers, servers, and application code.

How This Validator Works

This page helps you understand and troubleshoot JSON parsing failures by mapping the error message to common syntax rules. JSON is strict: keys must be in double quotes, strings must be properly escaped, values must use valid types, and punctuation must follow the expected structure. When a parser reports an unexpected token, it is usually telling you that the next character does not fit the grammar at that point in the document.

Common Validation Errors

Where This Validator Is Commonly Used

Why Validation Matters

Valid JSON is important because many systems depend on it as a machine-readable interchange format. A single syntax issue can break an API call, prevent a configuration file from loading, or cause a frontend application to fail when reading data. Validation helps teams catch formatting mistakes early, reduce debugging time, and avoid silent failures in production workflows. It also improves reliability when JSON is generated by templates, external services, or user input.

Technical Details

Format JSON (JavaScript Object Notation)
Parser behavior Parsing stops when a character does not match the expected JSON grammar
Common token types Strings, numbers, booleans, null, objects, arrays, punctuation
Typical error sources Malformed syntax, invalid escaping, truncated content, unsupported values
Related standards JSON syntax as commonly implemented by language parsers and APIs

In many environments, the error message includes a position, line number, or column number. That location is usually the best place to inspect first, but the actual mistake may be just before the reported token. For example, a missing quote earlier in the document can cause the parser to misread everything that follows.

FAQ

What does “unexpected token” mean in JSON?

It means the JSON parser found a character or symbol that is not valid at that point in the document. The token may be a letter, quote, comma, brace, or bracket. In practice, this usually points to a syntax issue such as a missing quote, trailing comma, or malformed value.

Why does JSON.parse() throw an unexpected token error?

JSON.parse() throws this error when the input string is not valid JSON. Common causes include single quotes, unescaped characters, extra commas, or non-JSON values like undefined. The parser expects strict JSON syntax and will stop as soon as it encounters something that does not fit the grammar.

Is an unexpected token error always caused by bad JSON?

Usually, yes, but not always in the same place the error is reported. Sometimes the real issue is earlier in the document, such as an unclosed string or missing delimiter. In other cases, the input may not be JSON at all, even if it looks similar to JavaScript object syntax.

What is the most common cause of JSON syntax errors?

One of the most common causes is using JavaScript object syntax instead of strict JSON. For example, JSON requires double quotes around keys and string values, and it does not allow comments or trailing commas. These differences often lead to unexpected token errors when data is copied from code into a JSON parser.

How do I find the exact location of the error?

Check the line, column, or character position reported by the parser if available. Then inspect the surrounding text for missing punctuation, unescaped quotes, or truncated content. If the parser reports a token near the end of the file, the issue may be an incomplete object, array, or string earlier in the payload.

Can HTML or special characters break JSON?

Yes, if they are inserted into a JSON string without proper escaping. Characters such as double quotes, backslashes, and control characters must be escaped. Raw HTML can also create problems if it contains unescaped quotes or line breaks inside a JSON string value.

What is the difference between JSON and JavaScript objects?

They look similar, but JSON is stricter. JSON only allows double-quoted strings, valid primitive values, and a limited set of punctuation. JavaScript objects can include single quotes, functions, comments, and undefined values, which are not valid in JSON. Copying object literals directly into JSON often causes parse errors.

Can a server send invalid JSON?

Yes. APIs sometimes return malformed JSON because of backend bugs, template issues, encoding problems, or partial responses. If a client receives invalid JSON, the parser will fail even if the request itself was correct. Checking the raw response body is often the fastest way to confirm the problem.

How can I prevent unexpected token errors?

Use a JSON validator during development, generate JSON with serializers instead of manual string building, and test API payloads before deployment. It also helps to lint configuration files, escape dynamic content correctly, and avoid mixing JavaScript syntax with JSON syntax in stored data or responses.

Related Validators & Checkers

FAQ

What does unexpected token mean in JSON?
The parser encountered a character it did not expect at that position, often a comma, quote, or bracket issue.
How do I find the unexpected token?
Use a JSON validator; it will report the line and column of the error.

Fix it now

Try in validator (prefill this example)

Related

All tools · Canonical