Fix JSON parse errors: trailing commas, quotes, and other gotchas
If you are staring at an error like Unexpected token or Invalid JSON, it usually comes down to one of five problems.
Start here:
- Open the JSON Formatter.
- Paste the JSON that is failing.
- Fix the first error it points to; reformat and try again.
JSON is stricter than JavaScript
Valid JSON looks like a JavaScript object, but it is not the same thing.
JavaScript lets you do things JSON does not:
- Single quotes:
'ok'instead of"ok" - Unquoted keys:
{ ok: true } - Comments:
// like this - Trailing commas:
{ "a": 1, }
If you copy from a JS file, a config snippet, or a blog post, you often pick up one of those.
Common JSON errors and the fix
Use this table when you want the fastest path to valid JSON.
| Symptom you see | What it usually means | Fix |
|---|---|---|
Error mentions Unexpected token ' in JSON | You used single quotes | Replace all string quotes with double quotes |
Error mentions Unexpected token / in JSON | You have a comment | Remove // and /* ... */ comments |
Error mentions Unexpected token } or Unexpected token ] | You have a trailing comma | Remove the comma before the closing brace/bracket |
Error mentions Unexpected token o in JSON (or another letter) | You pasted a bare word like ok or undefined | Wrap it in quotes if it is a string, or use true/false/null if that is what you meant |
| Your JSON looks fine, but parsing still fails | You have a hidden character | Recopy as plain text; remove zero-width spaces; retype the line |
When JSON breaks in different contexts
- Front-end debugging: Copying from console logs often pulls in single quotes and trailing commas. Run it through the formatter before dropping it into tests or fixtures.
- API responses: Some proxies return HTML error pages. If parsing fails and you see angle brackets, you received HTML, not JSON—check the status code first.
- Config files (VS Code, ESLint, tsconfig):
.jsoncallows comments; strict JSON parsers do not. Strip comments before bundling or sending to an API. - Analytics/events: Hidden characters from spreadsheets or chat exports are common. Normalize to plain text, then validate.
- Embedded JSON in HTML: If you copy from
<script type="application/json">, watch for escaped characters. Validate after unescaping.
Examples (invalid input, then the fix)
Trailing comma
Invalid JSON:
{
"name": "Ada",
"role": "admin",
}
Fix:
{
"name": "Ada",
"role": "admin"
}
Single quotes and unquoted keys
Invalid JSON:
{ user: 'maria', active: true }
Fix:
{ "user": "maria", "active": true }
Comments in the middle of JSON
Invalid JSON:
{
"env": "prod",
// turn on for debugging
"debug": false
}
Fix:
{
"env": "prod",
"debug": false
}
Prevent errors before they ship
- Format before commit: Enable format-on-save or a pre-commit hook; if formatting fails, you see the error early.
- Validate API fixtures: In tests, assert that
JSON.parsesucceeds on mocked or recorded payloads. - Avoid string splicing: Build objects in code and
JSON.stringifythem; concatenation is where stray commas creep in. - Keep comments out of production JSON: If you use comments locally, strip them or convert to a format that supports them before production.
Quick checklist before sending JSON to someone else
- Are all keys and strings double-quoted?
- Any trailing commas left?
- Did you remove
//and/* ... */comments? - Does it format cleanly in the JSON Formatter?
- If minified, does it still parse after using the JSON minifier?
How to use Textavia's JSON Formatter
- Open the JSON Formatter.
- Paste your JSON, even if it is minified.
- Pick an indent size (2 or 4 spaces), then copy the formatted output.
If the input is invalid, fix the first issue, then try again. JSON problems often cascade, and the first syntax error is the root cause.
Tips and proven approaches
- If you are debugging an API response, format it first; then compare versions with the diff tool.
- If you need to ship smaller payloads, format to make it readable, then minify it with the JSON minifier.
- If you suspect invisible characters, paste into a plain text field first (or use a plain text converter) before parsing.
Related tools
- Shrink formatted output for transport with the JSON minifier.
- Compare two payloads side by side with the diff tool.
- Decode Base64-wrapped JSON with the Base64 encoder/decoder.
Privacy and security
Textavia's JSON formatter runs locally in your browser. Your pasted JSON is processed on your device, not uploaded to a server.
