JSON looks simple until a parser rejects it with a message like Unexpected token } in JSON at position 1043. The error is almost always one of a handful of small mistakes, but the message doesn’t always point exactly at it. This guide explains what the common messages mean, why the reported position is sometimes misleading, and a reliable process for tracking down the real problem.
JSON’s rules in brief
JSON is stricter than the JavaScript it was based on. A valid JSON document follows these rules:
- Strings and object keys use double quotes, never single quotes.
- Every object key is a string in quotes:
{"name": "Ada"}, not{name: "Ada"}. - Items in objects and arrays are separated by commas, with no comma after the last item.
- The only values allowed are strings, numbers, objects, arrays,
true,falseandnull. There is noundefined,NaN,Infinity, date type or function. - Comments are not allowed, neither
//nor/* */. - Inside strings, a backslash starts an escape (
\",\\,\n,\u00e9), and raw line breaks and tab characters are not allowed. - Numbers can’t have leading zeros (
007), a leading plus sign, or a trailing decimal point (1.).
Almost every JSON error breaks one of these rules.
What the error messages mean
Different browsers and languages word their errors differently, but they fall into a few groups:
| Message (or similar) | Usual cause |
|---|---|
Unexpected token } or ] |
A trailing comma before a closing brace or bracket |
Expected property name or } |
A trailing comma in an object, or an unquoted key |
Unexpected token ' |
Single quotes instead of double quotes |
Unexpected token / |
A comment |
Expected , or } after property value |
A missing comma between two items |
| Unterminated string | A missing closing quote, or a raw line break inside a string |
| Unexpected end of JSON input | Truncated data: a missing closing bracket, or an empty response |
Unexpected token < at position 0 |
You received HTML (often an error page) instead of JSON |
The last one deserves special mention. If a server returns an error page, the “JSON” starts with <!DOCTYPE html>, and the parser fails on the very first character. The problem isn’t your JSON at all; check the request URL, authentication and the server’s status code.
Why the error position can be wrong
A parser reports the place where it first realised something was wrong, which isn’t always where the mistake is. Consider a missing closing quote:
{
"name": "Ada,
"role": "engineer"
}
The parser reads "Ada, and keeps going, treating the line break and the following text as part of the string, until it hits something it can’t accept. The reported error appears on line 2 or 3, after the actual mistake on line 2. The rule of thumb: look at the reported position, then look backwards. Missing quotes and brackets are usually found shortly before the point where the parser complained.
A reliable way to find the problem
- Format it. Paste the text into the JSON formatter and press Format. If it’s invalid, you’ll get the line and column, and the cursor jumps there. With one value per line, problems are much easier to see.
- Check the beginning and the end. Stray characters from copying (a log timestamp, a trailing semicolon, a
)]}'security prefix some APIs add) make the entire document invalid. - Look just before the reported position for a missing comma, quote or bracket.
- Halve the problem. For a large document, delete the second half of a big array or object (keeping the brackets balanced) and validate again. Keep halving until you find the item that breaks it.
- Check where the JSON came from. If a person edited it by hand, look for trailing commas and comments. If code generated it with string concatenation instead of a JSON library, look for unescaped quotes inside values.
Common mistakes and their fixes
Trailing commas. Very common when items are deleted or reordered by hand.
{"a": 1, "b": 2,} ✗
{"a": 1, "b": 2} ✓
Single quotes and unquoted keys. Often caused by copying a JavaScript object rather than real JSON.
{name: 'Ada'} ✗
{"name": "Ada"} ✓
Comments. Config files often need explanations, but JSON has no comment syntax. Remove the comments, move explanations into a "_comment" field, or switch to a format that allows comments if your tools support it, such as JSON5, JSONC (used by VS Code settings) or YAML.
Quotes inside strings. A double quote inside a string must be escaped:
{"title": "The "best" tool"} ✗
{"title": "The \"best\" tool"} ✓
Windows file paths. Backslashes start escape sequences, so "C:\new\file" silently becomes a line break (\n) and a form-feed character (\f) instead of the folder names you meant. Double every backslash: "C:\\new\\file", or use forward slashes.
Special number values. NaN, Infinity and undefined aren’t valid JSON. Use null, or a string if you need to keep the meaning.
Preventing JSON errors
The best fix is not writing JSON by hand at all. Generate it with your language’s JSON library (JSON.stringify in JavaScript, json.dumps in Python), which always produces valid output and escapes strings correctly. When you must edit JSON by hand, use an editor with JSON syntax highlighting, which marks errors as you type, and validate before you commit or deploy. A broken configuration file in production is a far more expensive way to discover a trailing comma.