Guide
How to Fix Invalid JSON: Common Errors and What They Mean
To fix invalid JSON, paste it into a JSON validator, read the position in the error message, and look just before that spot. Nine times out of ten the problem is one of a handful of mistakes: a trailing comma, single quotes instead of double quotes, an unquoted key, a missing comma, a comment, or a value like undefined or NaN that JSON doesn’t allow. Fix it, validate again, and repeat until it formats cleanly.
Try JSON Formatter free
Format, minify and validate JSON in your browser.
Runs in your browser. No upload, no sign-up.
The rules JSON is strict about
JSON looks like a JavaScript object, but its grammar (defined in RFC 8259) is much stricter. Most “invalid JSON” errors come from writing something that JavaScript, Python or a config file would accept but JSON doesn’t:
- Keys must be strings in double quotes:
"name", nevernameor'name'. - String values use double quotes too. Single quotes are not allowed anywhere.
- Items are separated by commas, with no comma after the last item in an object or array.
- Allowed values are only strings, numbers,
true,false,null, objects and arrays — lowercase, exactly as written. - No comments of any kind.
- A document contains one top-level value.
Step 1: Find where it breaks
- Paste the JSON into the JSON Formatter and press Format. It runs in your browser, so API keys or customer data in the payload aren’t uploaded.
- If it’s invalid, you’ll see the parser’s error message. Note the position, or the line and column if shown.
- Look at that spot and the character just before it. The parser reports where it *noticed* the problem, which is usually right after the actual mistake.
- Fix it and press Format again. Large files often have several errors; the parser stops at the first one each time.
Error wording depends on your browser
The message comes from your browser’s built-in JSON parser, so Chrome, Edge, Firefox and Safari word it differently. The examples below are the wording used by Chrome, Edge and Node.js; the cause and fix are the same everywhere.
Step 2: Match the error to the fix
| Invalid JSON | Error you’ll see | Fix |
|---|---|---|
{"age": 30,} | Expected double-quoted property name | Remove the trailing comma before } |
[1, 2, 3,] | Unexpected token ']' | Remove the trailing comma before ] |
{'name': 'Ana'} | Expected property name or '}' | Replace single quotes with double quotes |
{name: "Ana"} | Expected property name or '}' | Put the key in double quotes |
{"a": 1 "b": 2} | Expected ',' or '}' after property value | Add the missing comma |
{"a": 1 /* note */} | Expected ',' or '}' after property value | Delete the comment |
{"ok": True} | Unexpected token 'T' | Use lowercase true, false, null |
{"a": undefined} or NaN | Unexpected token 'u' / 'N' | Use null, or remove the key |
{"a": 012} | Unexpected number | Remove leading zeros, or make it a string |
{"a":1}{"b":2} | Unexpected non-whitespace character after JSON | Wrap the values in an array, or parse them line by line |
| (empty input) | Unexpected end of JSON input | The input is empty or cut off — check that the whole response was copied |
Problems inside strings
Some errors point into the middle of a string value. These are usually characters that must be escaped with a backslash:
- “Bad control character in string literal” — the string contains a real line break or tab. Replace a line break with
\nand a tab with\t. - “Bad escaped character” — a backslash is followed by a character that isn’t a valid escape, typically a Windows path like
C:\Users. Double every backslash:C:\\Users, or use forward slashes. - A quote inside a string ends the string early, and the parser then complains about the text after it. Escape inner quotes as
\". - Curly “smart” quotes (“like these”) look like quotes but aren’t. They sneak in when JSON is copied from a word processor, a chat app or a web page. Replace them with straight
"quotes.
Invisible characters
If the error points at the very first character and everything looks fine, the file may start with an invisible byte-order mark (BOM) or a zero-width space from copy-paste. Re-type the first bracket, or save the file as “UTF-8” rather than “UTF-8 with BOM”.
When the JSON came from somewhere else
- Python output:
print(my_dict)produces Python syntax (single quotes,True,None), not JSON. Usejson.dumps(my_dict)instead. - JavaScript objects: a logged object or a
.jsconfig isn’t JSON. UseJSON.stringify(obj, null, 2)to produce the real thing. - An API response that isn’t JSON at all: if the error mentions
<at position 0, you received an HTML page — usually an error page, a login redirect or a wrong URL. Check the status code and the endpoint. - Config files with comments: formats such as JSONC (used by VS Code and
tsconfig.json) and JSON5 allow comments and trailing commas, but a strict JSON parser rejects them. Strip them before parsing. - One JSON object per line: log files are often JSON Lines (NDJSON). Each line is valid on its own, but the whole file isn’t one JSON document — parse it line by line.
Avoiding broken JSON in the first place
- Generate JSON with a serializer (
JSON.stringify,json.dumpsand equivalents) instead of building it by hand with string concatenation. - Use an editor that highlights JSON syntax errors as you type.
- When two versions of a file behave differently, format both and compare them with Text Diff to spot the change.
- Minify JSON only for transport. Keep a formatted copy for editing and reviews.
Working with tokens? A JWT’s header and payload are Base64URL-encoded JSON — the JWT Decoder decodes them so you can read the claims.
Frequently asked questions
Are trailing commas allowed in JSON?
No. Standard JSON doesn’t allow a comma after the last item in an object or array. JavaScript, JSONC and JSON5 accept them, which is why they slip in so often.
Can JSON have comments?
Not in standard JSON. If you need notes, add a regular field such as "_comment", or use a format that supports comments (JSONC, JSON5, YAML) and convert it before parsing as JSON.
What does “Unexpected token < in JSON at position 0” mean?
Your code tried to parse an HTML page as JSON (newer browsers word it as “Unexpected token '<', "<!DOCTYPE "... is not valid JSON”). The server returned an error page or a redirect instead of data — check the URL, authentication and HTTP status of the request.
Does the formatter change my data?
Formatting only changes whitespace in almost every case. Because the JSON is parsed and re-written, a few edge cases can change: if a key appears twice only the last value is kept, numbers larger than about 9 quadrillion (2⁵³) can lose precision, and integer-like keys such as "1" and "2" are listed in numeric order.
Tools used in this guide
More guides
- How Many Words Is a 5-Minute Speech?A 5-minute speech is about 650–750 words at a natural pace. See word counts for 1 to 20 minutes, what changes your pace, and how to time your own script.
- How to Reduce PDF File Size for EmailPDF too big to email? Learn the real attachment limits for Gmail and Outlook, why PDFs get large, and how to shrink one quickly without ruining quality.
- How to Sign a PDF for Free (Without Printing It)Sign a PDF online in under a minute: draw, type or upload your signature, place it on the page and add the date. No printing, scanning or account needed.