Tech Help

Common JSON Errors: Trailing Commas, Quotes, and Unexpected Token

Fix invalid JSON: trailing commas, single quotes, unquoted keys, and Unexpected token. See why JSON.parse fails and how to validate JSON.

Quick Answer

Most JSON errors come from strict syntax rules: trailing commas, single quotes, unquoted property names, missing commas, mismatched brackets, bad escapes, or comments.

JSON looks like a JavaScript object, but it is stricter. Property names and strings need double quotes.

If you see “Unexpected token”, the reported character may be where parsing failed—not necessarily where the original mistake began.

Your JSON is marked invalid. That is usually a small syntax issue, not a broken file format. The seven mistakes below cause most Unexpected token errors.

Why Is JSON So Easy to Break?

JSON is a data format with strict syntax. A JavaScript object literal can look similar and still be invalid JSON. In standard JSON, property names need double quotes, strings need double quotes, trailing commas are not allowed, and comments are not part of the format.

1. Trailing Commas

The extra comma after the last property is the problem. Standard JSON does not allow it.

Invalid

{
  "name": "Alex",
  "age": 30,
}

Valid

{
  "name": "Alex",
  "age": 30
}

The same rule applies to arrays. Drop the comma after the last item.

Invalid

[
  "red",
  "green",
]

Valid

[
  "red",
  "green"
]

2. Single Quotes Instead of Double Quotes

JavaScript often accepts single quotes. JSON does not. Both property names and string values must use double quotes.

Invalid

{
  'name': 'Alex'
}

Valid

{
  "name": "Alex"
}

3. Unquoted Property Names

In JavaScript you can sometimes write name without quotes. In JSON every property name must be a double-quoted string.

Invalid

{
  name: "Alex",
  age: 30
}

Valid

{
  "name": "Alex",
  "age": 30
}

4. Missing Commas Between Values

Properties and array items need a comma between them. The parser may report Unexpected token on the next quote or brace, not on the missing comma itself.

Invalid

{
  "name": "Alex"
  "age": 30
}

Valid

{
  "name": "Alex",
  "age": 30
}

5. Missing or Extra Brackets and Braces

Objects use { }. Arrays use [ ]. Nested JSON often fails when a ] is missing, a } is extra, or the two are swapped. Count opening and closing pairs.

Invalid

{
  "items": [1, 2
}

Valid

{
  "items": [1, 2]
}

6. Invalid Escape Sequences

Inside a JSON string, a backslash starts an escape. Sequences such as ", \ and \n are valid. A Windows path written as C:\Users is not: \U is not a legal JSON escape. Double the backslashes, or the parser stops on that character.

Invalid

{"file":"C:\Users\Alex"}

Valid

{"file":"C:\\Users\\Alex"}

7. Comments Inside JSON

Standard JSON has no comments. Some config tools accept // or /* */ (JSONC, JSON5, and similar). That is not standard JSON, and JSON.parse will reject it.

Invalid

{
  // user name
  "name": "Alex"
}

What Does “Unexpected Token” Mean?

The parser met a character it did not allow at that position. The reported spot is where reading stopped. If you omitted a comma earlier, the error may point at the next property’s quote. Wording varies by runtime; do not expect every browser to print the same sentence.

What Does “Unexpected End of JSON Input” Mean?

The text ended before the value was complete. Typical causes include a missing } or ], truncated input, an empty body, or a cut-off API response. It is not one single bug.

Why Does JSON.parse() Fail?

JSON.parse() accepts valid JSON text only. This succeeds:

const text = '{"name":"Alex"}';
const data = JSON.parse(text);

This fails, because single quotes are not JSON:

const text = "{'name':'Alex'}";
JSON.parse(text);

The exact error message depends on the JavaScript runtime. Treat the message as a hint, then inspect commas, quotes, and brackets near that position.

JSON Is Not the Same as a JavaScript Object

A JavaScript object literal can look like { name: 'Alex' }. Valid JSON must look like { "name": "Alex" }: double-quoted names and strings, no trailing comma, no comments.

  • JSON text
  • Parser reads tokens
  • Valid data or Unexpected token
The error mark is where parsing stopped, which may be after the real mistake.

How to Find a JSON Error Quickly

Work in this order. Do not start by rewriting the whole file.

  1. Run the JSON through a validator.
  2. Read the first reported error.
  3. Look slightly before the reported position.
  4. Check commas, especially trailing and missing ones.
  5. Check quotes: double quotes only.
  6. Match { } and [ ].
  7. Check backslash escapes in strings.
  8. Remove comments if you need strict JSON.

Quick map of the usual fixes:

Problem Fix
Trailing commaDelete the comma after the last item.
Single quotesUse double quotes for names and strings.
Unquoted keyWrap the property name in double quotes.
Missing commaPut a comma between properties or array items.
Missing bracketClose every { } and [ ] pair.
Invalid escapeUse a legal escape, or double backslashes in paths.
CommentRemove // and /* */ for standard JSON.

Validate and Format JSON in Your Browser

NEXNARA JSON Formatter & Diff can check JSON on this device.

You can validate, pretty-print with 2 spaces, 4 spaces, or tabs, minify, compare two documents (JSON Diff), and open a .json file. Parsing uses JSON.parse in the browser, not eval.

Documents larger than 5 MB may feel slow. Text larger than 15 MB is not processed in the browser, so the page stays responsive.

JSON processing itself happens in your browser. The text is not sent to a NEXNARA server for this tool. Ads and other site features still use the network; that is separate from processing.

Need to check invalid JSON? Open JSON Formatter & Diff and paste the text.

FAQ

Why does my JSON say “Unexpected token”?

The parser hit a character it did not allow at that spot. The real mistake—often a missing comma or a single quote—may sit just before that position.

Are trailing commas allowed in JSON?

No. Standard JSON does not allow a comma after the last property or array item. JavaScript object literals often do.

Can JSON use single quotes?

No. Property names and strings must use double quotes.

Can JSON contain comments?

Not in standard JSON. Some tools accept JSONC or JSON5 with comments; JSON.parse will still reject them.

Why does JSON.parse() fail?

The string is not valid JSON. Typical causes are trailing commas, single quotes, unquoted keys, comments, or a truncated value. The exact message depends on the runtime.

How can I check whether JSON is valid?

Paste it into a validator such as JSON Formatter & Diff, or call JSON.parse in a console. If parsing throws, the text is not valid JSON.