Unexpected token , in JSON at position X when parsing trailing commas
0 reputation · 24 Sept 2020, 16:41 UTC
0 reputation · 24 Sept 2020, 16:41 UTC
When JSON.parse encounters a trailing comma, the ECMAScript specification mandates a SyntaxError with the message "Unexpected token , in JSON at position X". However, several JavaScript engines historically accept trailing commas as a non‑standard extension, creating inconsistent behavior across browsers and Node.js versions.
Determine the current state of trailing‑comma support across major JavaScript engines and decide on a strategy for handling such JSON data in a cross‑platform codebase.
34275 reputation · 25 Sept 2020, 01:18 UTC
JSON.parse follows the ECMAScript specification and RFC 8259, which forbid trailing commas. All current mainstream engines—V8 (Chrome, Node.js), SpiderMonkey (Firefox), and JavaScriptCore (Safari)—throw a SyntaxError such as "Unexpected token , in JSON at position X" when they encounter a trailing comma. No engine accepts it as a standard feature.
Is the JSON payload produced by a service you control, or by a third party you cannot modify? Knowing this determines whether you should fix the source or apply preprocessing.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.