Trailing comma in JSON
A comma separates two values. Put one directly before } or ] and JSON rejects it. The same line is perfectly legal in a JavaScript object or array, and has been since ES5, which is exactly why this one catches people out.
How the message appears
The same fault, worded by different engines. V8 (Chrome, Node, Edge) changed its format around Chrome 109, so older and newer runtimes disagree about the same document.
The fix
Remove the comma before ] and the comma before }. Nothing else changes.
{
"colors": [
"red",
"green",
],
"count": 2,
}{
"colors": [
"red",
"green"
],
"count": 2
}The example travels in the URL fragment, which browsers never send to a server.
What causes it
Hand-edited config files
You deleted the last entry in a list and left the comma sitting on the line above. This is the most common way a working config file suddenly stops loading.
Code generation with a loop
Something built the JSON by gluing strings together and put a comma after every item, including the last. Use JSON.stringify, or build an array and join it. A real serializer cannot make this mistake.
Copying from JavaScript source
Prettier and most formatters add trailing commas to JavaScript on purpose (trailingComma: "es5" and above). Copy one of those objects into a .json file and it stops parsing.
Worth knowing
- JSON5 and JSONC both allow trailing commas.
tsconfig.jsonand your VS Code settings are JSONC, not JSON. That is why the editor accepts them there andJSON.parsewill not. - Do not strip them with a find-and-replace. A comma inside a string value looks identical to a regex and would be destroyed too. The repair in the validator parses the document instead of pattern matching it.
Written by Vishnu Shankar.