{{jsSidebar("Errors")}}
Message
SyntaxError: JSON.parse: unexpected character at line x column y of the JSON data. SyntaxError: JSON.parse: unexpected end of data at line x column y of the JSON data. SyntaxError: JSON.parse: expected property name or '}' at line x column y of the JSON data SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line x column y of the JSON data SyntaxError: JSON.parse: unterminated fractional number at line 1 column 2 of the JSON data.
Error type
{{jsxref("SyntaxError")}}
What went wrong?
{{jsxref("JSON.parse()")}} parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.
Examples
JSON.parse()
does not allow trailing commas
Both lines will throw a SyntaxError:
JSON.parse('[1, 2, 3, 4, ]'); JSON.parse('{"foo" : 1, }'); // SyntaxError JSON.parse: unexpected character // at line 1 column 14 of the JSON data
Omit the trailing commas to parse the JSON correctly:
JSON.parse('[1, 2, 3, 4 ]'); JSON.parse('{"foo" : 1 }');
Property names must be double-quoted strings
You cannot use single-quotes around properties, like 'foo'.
JSON.parse("{'foo' : 1 }"); // SyntaxError: JSON.parse: expected property name or '}' // at line 1 column 2 of the JSON data
Instead write "foo":
JSON.parse('{"foo" : 1 }');
Leading zeros and decimal points
You cannot use leading zeros, like 01, and decimal points must be followed by at least one digit.
JSON.parse('{"foo" : 01 }'); // SyntaxError: JSON.parse: expected ',' or '}' after property value in object // at line 1 column 2 of the JSON data JSON.parse('{"foo" : 1. }'); // SyntaxError: JSON.parse: unterminated fractional number // at line 1 column 2 of the JSON data
Instead write just 1 without a zero and use at least one digit after a decimal point:
JSON.parse('{"foo" : 1 }'); JSON.parse('{"foo" : 1.0 }');
See also
- {{jsxref("JSON")}}
- {{jsxref("JSON.parse()")}}
- {{jsxref("JSON.stringify()")}}