Trailing comma in JSON
SyntaxError: Unexpected token ']', ..."green", ], "coun"... is not valid JSON
Where it stops
"green",
],
"count": 2,What it means
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.
Last verified on Chromium 145, Firefox 146 and WebKit 26.
It opens with the error marked. Nothing is uploaded. Find where the error is.
- Usually
- Hand-edited config filesCode generation with a loopCopying from JavaScript source
- Fix
- Remove the comma before
]and the comma before}.
The fix
Remove the comma before ] and the comma before }. Nothing else changes.
{
"colors": [
"red",
"green",
],
"count": 2,
}Lines 4 and 6 change.
{
"colors": [
"red",
"green"
],
"count": 2
}How the message appears
Chrome and Node first, then Firefox, then Safari.
- SyntaxError: Unexpected token ']', ..."green", ], "coun"... is not valid JSONChrome and Node
- SyntaxError: JSON.parse: unexpected character at line 5 column 3 of the JSON dataFirefox
- SyntaxError: JSON Parse error: Unexpected comma at the end of array expressionSafari
- SyntaxError: Expected double-quoted property name in JSON at position 8 (line 1 column 9)
- SyntaxError: JSON.parse: expected double-quoted property name at line 1 column 9 of the JSON data
- SyntaxError: JSON Parse error: Property name must be a string literal
- SyntaxError: Unexpected token ']', "[1,]" is not valid JSON
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.jsonfile and it stops parsing.
Worth knowing
Watch outDo 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.
NoteJSON5 and JSONC both allow trailing commas. tsconfig.json and your VS Code settings are JSONC, not JSON. That is why the editor accepts them there and JSON.parse will not.