Last active
January 20, 2022 09:53
-
-
Save pmstss/0cf3a26dd3c805389ef583c0279532e7 to your computer and use it in GitHub Desktop.
Chrome/FF differences in SyntaxError wording on JSON.parse()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/JSON_bad_parse | |
let errorCases = [ | |
{ | |
source: '[1, 2, 3, 4,]', | |
cause: 'JSON.parse() does not allow trailing commas', | |
mdn: `SyntaxError JSON.parse: unexpected character`, | |
ff: `JSON.parse: unexpected character at line 1 column 13 of the JSON data`, | |
node: `Unexpected token ] in JSON at position 12` | |
}, | |
{ | |
source: '{"foo": 1,}', | |
cause: 'JSON.parse() does not allow trailing commas', | |
mdn: `SyntaxError JSON.parse: unexpected character`, | |
ff: `JSON.parse: expected double-quoted property name at line 1 column 11 of the JSON data`, | |
node: `Unexpected token } in JSON at position 10`, | |
}, | |
{ | |
source: "{'foo': 1}", | |
cause: 'Property names must be double-quoted strings', | |
mdn: `SyntaxError: JSON.parse: expected property name or '}'`, | |
ff: `JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data`, | |
node: `Unexpected token ' in JSON at position 1` | |
}, | |
{ | |
source: '{"foo": 01}', | |
cause: 'Leading zeros and decimal points', | |
mdn: `SyntaxError: JSON.parse: expected ',' or '}' after property value`, | |
ff: `JSON.parse: expected ',' or '}' after property value in object at line 1 column 10 of the JSON data`, | |
node: `Unexpected number in JSON at position 9` | |
}, | |
{ | |
source: '{"foo": 1.}', | |
cause: 'Leading zeros and decimal points', | |
mdn: `SyntaxError: JSON.parse: unterminated fractional number`, | |
ff: `JSON.parse: unterminated fractional number at line 1 column 11 of the JSON data`, | |
node: `Unexpected token } in JSON at position 10` | |
}, | |
{ | |
source: '{"a": "b}', | |
cause: 'Unterminated string', | |
mdn: ``, | |
ff: `JSON.parse: unterminated string at line 1 column 16 of the JSON data`, | |
node: `Unexpected end of JSON input` | |
}, | |
{ | |
source: '{"}', | |
cause: 'Unexpected end of JSON input', | |
mdn: ``, | |
ff: `Unexpected end of JSON input`, | |
node: `Unexpected end of JSON input` | |
} | |
]; | |
errorCases.forEach(errorCase => { | |
try { | |
JSON.parse(errorCase.source); | |
} catch (e) { | |
console.dir({ ...errorCase, actual: e.message }); | |
console.log('\n'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment