Common JSON Errors & How to Fix Them

JSON is strict. A single misplaced comma, wrong quote, or invalid character can break parsing. This guide covers the 10 most common JSON syntax errors and how to fix them.

1. Trailing comma after last item

JSON does not allow a comma after the last element in an object or array. This is one of the most common mistakes, especially when copying from JavaScript.

Bad:

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

Correct:

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

How to fix: Remove the comma after the last property or array element.

2. Single quotes instead of double quotes

JSON requires double quotes for all strings and property keys. Single quotes are invalid.

Bad:

{'name': 'Alice', 'active': true}

Correct:

{"name": "Alice", "active": true}

How to fix: Replace all single quotes with double quotes. Use a find-and-replace or a JSON formatter.

3. Unquoted property keys

Property names must be enclosed in double quotes. Unquoted keys like JavaScript object shorthand are invalid.

Bad:

{name: "Alice", age: 30}

Correct:

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

How to fix: Wrap every property key in double quotes.

4. Missing comma between items

Each property or array element must be separated by a comma. Forgetting a comma causes a parse error.

Bad:

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

Correct:

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

How to fix: Add a comma between each pair of properties or array elements.

5. Comments in JSON (not allowed)

JSON does not support comments. Neither // nor /* */ are valid.

Bad:

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

Correct:

{
  "name": "Alice"
}

How to fix: Remove all comments. Use a separate field like "_comment" if you need documentation in the data.

6. Undefined/NaN values

JSON only supports null, not undefined. NaN and Infinity are also invalid.

Bad:

{"value": undefined, "ratio": NaN}

Correct:

{"value": null, "ratio": null}

How to fix: Use null for missing or invalid values. Omit the key if the value is undefined.

7. Unclosed brackets or braces

Every { must have a matching }, and every [ must have a matching ]. Missing one breaks parsing.

Bad:

{"items": [1, 2, 3

Correct:

{"items": [1, 2, 3]}

How to fix: Count opening and closing brackets. Ensure each { has } and each [ has ].

8. Extra comma in arrays

A comma with nothing after it (or before the first element) is invalid.

Bad:

[1, 2, 3,]

Correct:

[1, 2, 3]

How to fix: Remove trailing or leading commas. Same rule as trailing comma in objects.

9. Using JavaScript syntax (functions, variables)

JSON is data only. No functions, template literals, or variable references.

Bad:

{"fn": function(){}, "date": new Date()}

Correct:

{"date": "2025-03-07T12:00:00.000Z"}

How to fix: Convert functions to strings or omit. Serialize dates as ISO 8601 strings.

10. Incorrect escape sequences

Only certain escape sequences are valid: \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX.

Bad:

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

Correct:

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

How to fix: Double backslashes for literal backslashes. Use \uXXXX for Unicode.

Fix JSON instantly

Paste your JSON into our formatter to validate, format, and identify errors.

Open JSON Formatter

Frequently Asked Questions

Why does JSON not allow trailing commas?

JSON was designed to be a minimal, unambiguous data format. Trailing commas can cause parser ambiguity in some implementations. The JSON spec (RFC 8259) explicitly forbids them. JavaScript allows trailing commas in objects and arrays, which is why this error is common when copying from JS.

Can I use a tool to fix JSON automatically?

Yes. A JSON formatter or validator can often fix simple issues like trailing commas, single quotes, and unquoted keys. For complex errors (unclosed brackets, wrong escape sequences), you may need to fix them manually. DuskTools JSON Formatter can format and validate JSON — try pasting your broken JSON to see the exact error location.

What's the difference between JSON and JavaScript objects?

JSON is a subset of JavaScript object literal syntax. JSON requires double quotes for keys and strings, allows only null (not undefined), and forbids comments, trailing commas, and functions. JavaScript is more permissive. Use JSON.stringify() to convert a JS object to valid JSON.

How do I escape special characters in JSON strings?

Use backslash: \" for quotes, \\ for backslash, \n for newline, \t for tab, \r for carriage return, \/ for forward slash (optional). For Unicode: \uXXXX (4 hex digits). Invalid escapes like \x or single \ will cause parse errors.

Why does my API return 'Unexpected token' errors?

This usually means invalid JSON syntax — often a trailing comma, single quote, unquoted key, or wrong escape. Check the raw response in your browser's Network tab or use a JSON validator to pinpoint the error. The error message often includes the character position.

Related Tools