What the error means
A comma after the last property of an object or the last item of an array is allowed in JavaScript but not in JSON. After the comma the parser expects another key (objects) or value (arrays) and finds a closing brace or bracket instead.
The message looks like this in Chrome, Edge and Node.js (Firefox and Safari use different wording for the same problems):
Expected double-quoted property name in JSON at position 8 (line 1 column 9)
Unexpected token ']', "[1, 2, 3,]" is not valid JSONCommon causes
A comma after the last property
This is the classic mistake when you delete the last line of an object and keep the comma of the line above it. The error names the missing key.
Broken: {"a": 1,}
Valid: {"a": 1}A comma after the last array item
Arrays fail with an error that mentions the closing bracket.
Broken: [1, 2, 3,]
Valid: [1, 2, 3]A file that is really JSONC or JSON5
tsconfig.json, VS Code settings and many tool configs allow comments and trailing commas because their own parsers accept JSONC. JSON.parse does not, so reading such a file with JSON.parse fails. Use a parser that supports JSONC or JSON5, or strip the extras first.
Generated text with a loop that always adds a comma
Building JSON by string concatenation easily leaves a final comma. Build objects and arrays in code and serialise them with JSON.stringify.
How to find the problem
- Paste the text that you passed to JSON.parse into the editor above. The status bar shows the line and column of the first problem, and Go to error jumps there.
- Remember that the position in the message is where the parser gave up, which is often one token after the real mistake. Look at the character before it as well.
- If the text came from a request, log the raw response text before parsing it. In the browser, open the Network tab, select the request and read the Response tab.
Never build JSON by hand
// Fragile: leaves a trailing comma
let out = "[";
for (const x of items) out += JSON.stringify(x) + ",";
out += "]";
// Correct
const out = JSON.stringify(items);Frequently asked questions
Why do some editors accept trailing commas?
They treat the file as JSONC or JSON5. That is a superset of JSON that JSON.parse and most APIs do not accept.
Will Fix JSON remove them?
Yes. Fix JSON removes trailing commas along with other common problems such as comments and single quotes.