Developer

Common JSON Syntax Errors and How to Fix Them: Unexpected Token, Trailing Commas, and Unquoted Keys

Debug JSON parsing failures in seconds. Learn the strict rules of RFC 8259, how to fix trailing commas, single quotes, unescaped characters, and NaN values.

The Synctoolo Team··9 min read
Developer writing software code and fixing syntax bugs on a computer

Every developer has encountered the dreaded JavaScript error: SyntaxError: Unexpected token in JSON at position 142. Because JSON is derived from JavaScript object literal syntax, developers often assume it follows the same forgiving rules. It does not.

JSON (defined formally by RFC 8259 and ECMA-404) is an exceptionally strict data interchange format. A single stray comma, an unescaped newline, or single quotation marks will instantly break parsers in Python, Go, Java, and browser environments.

This guide highlights the most frequent JSON syntax mistakes, explains why standard parsers reject them, and demonstrates how to repair them instantly using our private, client-side JSON Formatter and Validator.

Valid JSON vs JavaScript Object Literals: The Key Differences

The core source of confusion is that valid JavaScript syntax is frequently invalid JSON:

Syntax Rule JavaScript Object Literal Strict JSON (RFC 8259)
Key quotation Optional ({ name: 'Alice' }) Mandatory double quotes ({ "name": "Alice" })
String quotation Single or double quotes or backticks Double quotes ONLY ("value")
Trailing commas Allowed ([1, 2, 3,]) Strictly forbidden ([1, 2, 3])
Comments Allowed (// comment) Strictly forbidden
Special values NaN, undefined, Infinity Forbidden (Use null or numbers)
Multiple monitors displaying syntax highlighted code in an engineering workspace
Client-side JSON validators highlight syntax errors at the exact row and column without sending data to a remote server. Photo by Fotis Fotopoulos on Unsplash.

The 4 Most Common JSON Syntax Errors and Solutions

1. Trailing Commas in Arrays and Objects

In modern ECMAScript, trailing commas make git diffs cleaner. But in JSON, placing a comma after the final property throws an immediate parse error:

// INVALID JSON
{
  "api_version": 2,
  "status": "active",
}

// VALID JSON
{
  "api_version": 2,
  "status": "active"
}

2. Single Quotes Instead of Double Quotes

Python dictionaries and JavaScript strings allow single quotes. JSON strictly mandates double quotes (ASCII 34) for both property keys and string values:

// INVALID JSON
{ 'user': 'dev_401' }

// VALID JSON
{ "user": "dev_401" }

3. Unescaped Control Characters and Newlines

If your string contains raw line breaks or tabs, the parser will fail with an unexpected token error. String content must use escape sequences such as \n for newlines and \t for tabs:

// INVALID JSON
{
  "description": "First line
Second line"
}

// VALID JSON
{
  "description": "First line\nSecond line"
}

4. Number Formatting Rules (Leading Zeroes and Hexadecimals)

JSON numbers cannot have leading zeroes (e.g. 05 is rejected), cannot start with a decimal point without a leading zero (e.g. .5 must be written as 0.5), and do not support hexadecimal notation (0xFF).

Need to validate a large payload with precise line and character error reporting? Open Synctoolo's free JSON Formatter. It parses your input in local browser memory, highlights syntax problems, and formats your data cleanly.

Tools mentioned in this article

FAQ

Why does JSON reject trailing commas?+

The RFC 8259 JSON specification explicitly defines comma as a separator between values, not a terminator. If a comma appears after the final item, the parser expects another value and throws an unexpected end of input error.

Can JSON files contain comments?+

Standard JSON does not support comments. Douglas Crockford removed comments from the specification to prevent developers from adding parsing directives that would break interoperability. JSON5 and JSONC support comments, but standard parsers will fail.

Why does JSON require double quotes instead of single quotes?+

The JSON standard specifies double quotation marks (ASCII 34) exclusively to ensure uniform parsing behavior across all programming languages, many of which treat single quotes as single-character literals rather than strings.

How does Synctoolo JSON Formatter protect confidential JSON payloads?+

Synctoolo JSON Formatter runs entirely on the client side using your web browser's JavaScript engine. No payload data is ever sent across the network to external servers, ensuring complete privacy for database dumps and credentials.

S
The Synctoolo Team

We build and review free, privacy-first tools at Synctoolo.

Keep reading