JSON syntax errors and how to fix them
JSON is the most common format applications and APIs use to exchange data. Its rules are simple but strict; one missing quote makes the whole file unreadable.
The most common mistakes
- Single quotes: strings and keys must use double quotes (").
- Trailing commas: a comma after the last item is fine in JavaScript but an error in JSON.
- Comments: // and /* */ are not supported.
- Unquoted keys: {name: "Ann"} is invalid; it must be {"name": "Ann"}.
- JavaScript values: undefined, NaN and Infinity are not JSON values.
What “Unexpected token” means
The parser met a character it did not expect at that position. The real mistake is often on the line before; a forgotten comma shows up as an error at the start of the next line.
Large numbers
JavaScript cannot store integers longer than 16 digits exactly. Keeping long IDs such as order or user numbers as strings avoids data loss.
Readability
Pretty-printing one-line JSON makes the structure easy to see; minifying it for production keeps it small.
The JSON formatter and validator shows the line and column of an error and explains the likely cause.
Comments0
No Comments Yet
Be the first to share your thoughts.
Please sign in or sign up to leave a comment.