How to Fix Common JSON Syntax Errors

Most JSON errors come from a short list of mistakes: trailing commas, single quotes, unquoted keys, missing brackets, and comments. If you are staring at a “SyntaxError: Unexpected token” message, the cause is almost certainly one of these. Here is each one, what it looks like, and exactly how to fix it.

The fastest way to find the problem

Before going line by line, paste your JSON into a validator. A good JSON formatter and validator will point you to the position of the error, which usually sits right after the real mistake, and saves you hunting through hundreds of lines by eye. With the location in hand, the fixes below take seconds. Everything below also explains why each error happens, so you stop making it.

1. Trailing commas

This is the single most common JSON error. JSON does not allow a comma after the last item in an object or array, even though JavaScript does. That habit from writing JS objects carries over and breaks the JSON.

// INVALID, trailing comma after the last item
{
  "name": "CodeZips",
  "year": 2019,
}

// VALID, remove the trailing comma
{
  "name": "CodeZips",
  "year": 2019
}

The fix is simply to delete the comma after the final key-value pair, and likewise after the last element of an array. If an error points to a closing } or ], a trailing comma just before it is the usual culprit.

2. Single quotes instead of double quotes

JSON requires double quotes for all strings and all keys. Single quotes are not valid JSON, even though they are perfectly fine in JavaScript and Python. This trips people up constantly when copying data out of code.

// INVALID, single quotes
{ 'name': 'CodeZips' }

// VALID, double quotes
{ "name": "CodeZips" }

The fix is to replace every single quote with a double quote. If your strings contain apostrophes, escape them or rely on the double quotes around the whole string to contain them.

3. Unquoted keys

Every key in a JSON object must be a quoted string. In JavaScript you can write an object key without quotes, but JSON does not allow it.

// INVALID, unquoted key
{ name: "CodeZips" }

// VALID, quoted key
{ "name": "CodeZips" }

The fix is to wrap every key in double quotes. This is one of the clearest signs that someone pasted a JavaScript object literal and expected it to be valid JSON.

4. Missing or mismatched brackets

Every opening brace { needs a matching closing }, and every opening bracket [ needs a matching ]. A single missing or extra bracket throws the whole structure off, and the error often points to the very end of the document because that is where the parser finally gives up.

// INVALID, missing closing brace
{
  "user": {
    "name": "CodeZips"
}

// VALID, both objects closed
{
  "user": {
    "name": "CodeZips"
  }
}

The fix is to count your brackets, or better, format the JSON so the indentation reveals where a level was never closed. A formatter makes a missing bracket jump out immediately because the indentation will look wrong.

5. Comments

Standard JSON does not support comments of any kind, neither // nor /* */. If your file has them, it is technically a config dialect like JSONC, not strict JSON, and a standard parser will reject it.

// INVALID, comments are not allowed in JSON
{
  "name": "CodeZips", // this comment breaks it
}

// VALID, no comments
{
  "name": "CodeZips"
}

The fix is to remove all comments. If you need to annotate the data, add a string field such as "_comment": "note here" instead, which is valid JSON.

6. Wrong value types and stray characters

A few smaller issues round out the list. Numbers must not have leading zeros or a trailing decimal point with nothing after it. Strings must use the proper escape sequences, so a literal newline inside a string is invalid; use \n. Values like undefined or JavaScript functions are not valid JSON at all; only strings, numbers, true, false, null, objects, and arrays are allowed. And an invisible stray character, such as a non-breaking space pasted from a web page, can break parsing even when everything looks correct.

The “Unexpected token” message decoded: JavaScript’s parser reports the first character it could not make sense of. That character is rarely the real problem; the real problem is usually just before it, a missing comma, an unclosed bracket, or a stray quote. Look at what comes right before the reported position.

Why JSON is this strict

It can feel pedantic that JSON rejects things JavaScript happily accepts. The strictness is deliberate. JSON is a data interchange format meant to be parsed identically by every language and system, so it keeps its rules minimal and unambiguous. The features it omits, trailing commas, comments, single quotes, are exactly the ones that vary between languages and would create inconsistency. Understanding that JSON is intentionally a strict subset of JavaScript object syntax, not the same thing, prevents most of these errors before they happen.

Fix it instantly: paste your broken JSON into the free CodeZips JSON formatter and validator. It pinpoints the error location, validates as you type, and formats valid JSON cleanly. It runs entirely in your browser, so nothing you paste is uploaded.

Frequently asked questions

How do I fix a JSON syntax error?

Paste the JSON into a validator to find the error location, then check for the common causes: a trailing comma, single quotes instead of double, an unquoted key, a missing bracket, or a comment. Fixing the issue at the reported position, or just before it, resolves most errors.

Why does JSON say “Unexpected token”?

It means the parser hit a character it did not expect. The real mistake is usually just before that point, often a missing comma or an unclosed bracket. Look at the character immediately before the reported position.

Can JSON have comments?

No. Standard JSON does not allow comments. If you need a note, add it as a regular string field like "_comment". Files with real comments are a different format such as JSONC, not strict JSON.

Why are trailing commas not allowed in JSON?

JSON is a strict, language-neutral format, and trailing commas are one of the inconsistencies it deliberately omits so every parser behaves identically. Remove the comma after the last item in any object or array.

Why does my JavaScript object fail as JSON?

JavaScript object syntax is more permissive than JSON. It allows unquoted keys, single quotes, trailing commas, and comments, none of which are valid JSON. Convert each of those to the strict JSON form to fix it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top