Learn/Working with JSON

JSON Errors — How to Find & Fix Them Fast

Every developer has stared at “Unexpected token” wondering what went wrong. This guide covers the 15 most common JSON errors, shows you exactly how to diagnose them, and gives you the fix every time.

Beginner~12 min read

How JSON Parsing Errors Work

When JSON.parse() encounters invalid syntax, it throws a SyntaxError with a message describing the problem and the character position where parsing failed. The key to fast debugging is understanding what each error message actually means.

Error anatomyjavascript
1try {
2 JSON.parse('{"name": "Alice",}');
3} catch (err) {
4 console.log(err.message);
5 // "Expected double-quoted property name in JSON at position 17"
6 // Position 17 = the closing brace } right after the trailing comma
7}

The 15 Most Common JSON Errors

1

Single quotes instead of double quotes

Broken

{'name': 'Alice'}

Fixed

{"name": "Alice"}

JSON requires double quotes for all strings and keys. Single quotes are valid in JavaScript but not in JSON.

2

Trailing comma

Broken

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

Fixed

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

A comma after the last property or array item is invalid. JavaScript allows it, JSON does not.

3

Unquoted keys

Broken

{name: "Alice"}

Fixed

{"name": "Alice"}

All object keys must be strings enclosed in double quotes.

4

Comments in JSON

Broken

{ // user name "name": "Alice" }

Fixed

{ "name": "Alice" }

JSON has no comment syntax. Use JSONC for configs that need comments, or move docs to a separate file.

5

Unescaped newlines in strings

Broken

{"bio": "Line 1 Line 2"}

Fixed

{"bio": "Line 1\nLine 2"}

Literal newline characters inside strings are invalid. Use the \\n escape sequence.

6

Unescaped backslashes

Broken

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

Fixed

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

Backslashes must be doubled. A single backslash starts an escape sequence.

7

Using undefined or NaN

Broken

{"value": undefined}

Fixed

{"value": null}

undefined, NaN, and Infinity are not valid JSON values. Use null for absent values.

8

Leading zeros on numbers

Broken

{"code": 007}

Fixed

{"code": 7}

Numbers cannot have leading zeros in JSON (except 0 itself and 0.x decimals).

9

Capitalized booleans or null

Broken

{"active": True, "x": Null}

Fixed

{"active": true, "x": null}

JSON uses lowercase true, false, and null. Capitalized versions are not valid.

10

Missing comma between items

Broken

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

Fixed

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

Key-value pairs and array items must be separated by commas.

11

Extra comma between items

Broken

[1,, 3]

Fixed

[1, 3]

Double commas create an implicit undefined slot, which JSON does not support.

12

Unclosed string

Broken

{"name": "Alice}

Fixed

{"name": "Alice"}

Every opening double quote must have a matching closing quote on the same line.

13

Truncated JSON

Broken

{"users": [{"id": 1

Fixed

{"users": [{"id": 1}]}

Every opening brace and bracket must be closed. Truncated data often comes from network timeouts.

14

BOM (Byte Order Mark)

Broken

{"name": "Alice"}

Fixed

{"name": "Alice"}

Some text editors add an invisible BOM character at the start. Strip it before parsing.

15

Mixing JSON with JavaScript

Broken

{name: "Alice", fn: () => {}}

Fixed

{"name": "Alice"}

JSON is data only. Functions, methods, and unquoted identifiers are JavaScript, not JSON.

Debugging Strategy

Step 1: Locate the Error Position

Most parsers report the character position or line number. Start there and look for the patterns above.

Step 2: Use a Validator Tool

Paste your JSON into a JSON Validator to get line-by-line error reports with clear messages.

Step 3: Simplify and Isolate

If the JSON is large, cut it in half and validate each half separately. This binary search narrows the error quickly.

Step 4: Check Encoding

Open the file in a hex editor or check Content-Type headers. JSON must be UTF-8.

Prevention Checklist

Always use double quotes for keys and strings
Never add trailing commas
Escape backslashes and special characters
Use null instead of undefined
Validate JSON before sending over the network
Set Content-Type: application/json in API responses
Use a linter in your editor (VS Code has built-in JSON validation)
Run JSON through a formatter before committing to version control

Try It — Fix This Broken JSON

This JSON has five errors. Can you fix all of them?

Try It Yourself

Fix single quotes, trailing comma in array, capitalized boolean, comment, and undefined

Frequently Asked Questions

What does "Unexpected token" mean in JSON?
It means the parser encountered a character it did not expect at that position. Common causes: single quotes instead of double quotes, unquoted keys, trailing commas, or comments in JSON.
How do I find which line has a JSON error?
Use a JSON validator tool that reports line and column numbers. Most browser dev tools and code editors also highlight JSON syntax errors with exact positions.
Can I add comments to JSON?
Standard JSON (RFC 8259) does not support comments. Use JSONC or JSON5 for files that need comments, or store documentation in a separate file.
Why does my JSON fail with a trailing comma?
JSON does not allow a comma after the last item in an object or array. This is valid in JavaScript but not in JSON. Remove the trailing comma to fix the error.
How do I fix "Unexpected end of JSON input"?
This means the JSON string is truncated or incomplete. Check for missing closing braces }, brackets ], or quotes. The data may have been cut off during transfer.