Causes and fixes
| Invalid JSON | Error | Fix |
|---|---|---|
{"a": 1,} | Expected double-quoted property name | Remove the trailing comma. |
{a: 1} | Expected property name or '}' | Quote the key: {"a": 1}. |
{'a': 1} | Expected property name or '}' | Use double quotes. |
{"a": 1, // note | Expected double-quoted property name | Remove the comment. |
Config files with comments
Files like tsconfig.json and VS Code's settings.json are JSONC (JSON with comments), which allows comments and trailing commas. JSON.parse can't read them. Use a JSONC parser such as the jsonc-parser package, or remove the comments and trailing commas first.
Copied from JavaScript code?
An object literal from source code isn't JSON. Convert it in the browser console, which prints valid JSON:
copy(JSON.stringify({ name: 'Ada', age: 36, }, null, 2));
// Clipboard now contains:
// {
// "name": "Ada",
// "age": 36
// }Frequently asked questions
Why doesn't JSON allow trailing commas?
JSON was designed to be minimal and has never changed its grammar, so every parser in every language reads it the same way. JavaScript, JSON5 and JSONC allow trailing commas; strict JSON does not.
How do I find the position in a large file?
The error gives a character position, which is hard to count by hand. Paste the JSON into the validator, which turns it into a line and column.
Can a single quote ever appear in JSON?
Inside a string, yes: "it's" is valid. Only the quotes around keys and string values must be double quotes.