How to Fix the Most Common JSON Syntax Errors
6 min read · Published 1 November 2026 · Updated 2 November 2026
Almost every JSON error message looks the same: "Unexpected token" or "Unexpected end of JSON input", sometimes with a position number that means little at first glance. The good news is that real-world JSON breaks in a small number of repeatable ways. Learn these and you can usually fix a broken file in under a minute.
In this guide
- How to read the error
- 1. Trailing commas
- 2. Single quotes and unquoted keys
- 3. Comments
- 4. undefined, NaN and Infinity
- 5. Numbers: leading zeros, hex and very large values
- 6. Unescaped characters inside strings
- 7. Invisible and look-alike characters
- 8. Truncated or unbalanced data
- Preventing errors at the source
- What common error messages mean
- A debugging routine that works
How to read the error
The parser stops at the first character it cannot accept, so the reported position is where parsing failed, not always where the mistake was made. A missing quote or comma often only shows up a few characters later. Paste the text into a formatter that highlights the failing line, fix the first error, and check again, because later errors can disappear once the first one is fixed.
1. Trailing commas
JSON does not allow a comma after the last item of an object or array. This is the single most common mistake, because many programming languages and some configuration formats do allow it.
Invalid → valid
{ "name": "Lan", "age": 30, } // invalid: comma after 30
{ "name": "Lan", "age": 30 } // valid2. Single quotes and unquoted keys
Strings and keys must use double quotes. JavaScript object literals accept single quotes and bare keys, so code copied from a script often fails when pasted as JSON.
Invalid → valid
{ name: 'Lan' } // invalid
{ "name": "Lan" } // valid3. Comments
Standard JSON has no comments. Remove // and /* */ comments before parsing. If you need notes in a configuration file, either use a field such as "_comment" that your application ignores, or choose a configuration format that supports comments.
4. undefined, NaN and Infinity
These are JavaScript values, not JSON values. JSON only has null, true, false, numbers, strings, arrays and objects. When a script serialises an object, undefined fields are dropped and NaN or Infinity become null, so check the source of the data if values are missing or unexpectedly null.
5. Numbers: leading zeros, hex and very large values
- Leading zeros such as 007 are not valid numbers. Write 7, or use a string if the leading zero matters, for example a postal code or phone number.
- Hexadecimal (0xFF), a leading plus sign (+5) and a trailing decimal point (5.) are not valid JSON numbers.
- Integers above 9007199254740991 (2^53 − 1) cannot be stored exactly by JavaScript numbers. IDs from some databases and APIs exceed this limit and get silently rounded. If exact digits matter, transmit such IDs as strings.
6. Unescaped characters inside strings
A string cannot contain a raw line break or tab. Write them as \n and \t. A literal backslash must be written \\, and a double quote inside a string must be \". File paths on Windows are a classic trap, because C:\Users must be written C:\\Users.
7. Invisible and look-alike characters
Text copied from documents, chat apps and web pages may contain curly "smart quotes", non-breaking spaces or a byte order mark at the start of the file. They look correct but are not valid JSON characters. If a document looks perfect and still fails, retype the quotes or run the text through a tool that normalises it.
8. Truncated or unbalanced data
"Unexpected end of JSON input" usually means the text was cut off, for example by a failed download, a size limit in a log viewer or an incomplete copy. Count the opening and closing braces and brackets, or open the data in a tree view where unbalanced structure is easy to spot.
Preventing errors at the source
- Never build JSON by joining strings. Use the serialiser of your language, such as JSON.stringify, which escapes everything correctly.
- Validate input as early as possible and return the position of the error to the user.
- Keep a formatter handy to pretty print responses while debugging, so structure problems become visible.
What common error messages mean
The wording of a parse error differs between browsers, Node.js and other languages, and changes between versions, but the families of message are stable:
- "Unexpected token" (browsers, Node.js): the parser found a character that is not allowed at that point, often a trailing comma, a single quote or a stray character such as a comment.
- "Unexpected end of JSON input": the text stopped too early, so a bracket or quote is missing or the data was truncated.
- "Expecting property name enclosed in double quotes" (Python): an object key is unquoted or uses single quotes, or there is a trailing comma before the closing brace.
- "Expecting ',' delimiter" (Python): two values are next to each other without a comma, or a closing quote or bracket is missing earlier in the text.
A debugging routine that works
- Paste the text into a JSON formatter so it points to the first line that fails.
- Fix only that first error, then check again. One mistake often causes several messages.
- If the formatter accepts the text, but your program still fails, compare what your program actually receives (the raw response body) with what you pasted. Encoding or proxy changes are common causes.
- If the text came from a log or terminal, check for truncation and for added prefixes such as timestamps.
- Once it parses, validate the structure (the keys and types you expect), because valid JSON can still be the wrong shape.
Frequently asked questions
- Why does my code accept this "JSON" but a validator rejects it?
- Many programming languages accept a looser object-literal syntax that allows single quotes, unquoted keys, comments and trailing commas. A real JSON parser follows the stricter specification, so text that works as code can still be invalid JSON.
- What happens with duplicate keys?
- The specification says names within an object should be unique, and the behaviour for duplicates is not defined. Most parsers keep the last value, but you should not rely on it. Avoid duplicates.
- Is JSON5 or JSONC the same as JSON?
- No. They are supersets that add comments, trailing commas and more relaxed quoting. They are convenient for hand-edited configuration but must be converted to strict JSON before being sent to a system that expects JSON.
- Is there a size limit?
- The format itself has none. Practical limits come from memory, from the tool or library you use and from the servers and proxies the data passes through.
Sources and further reading
Try it yourself: JSON Formatter & Validator runs in your browser, free and with no sign-up.