Sometimes, we may run into the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps.
Fix the ‘SyntaxError: JSON.parse: bad parsing’ When Developing JavaScript Apps
To fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps, we should make sure we pass in a valid JSON string as an argument of the JSON.parse
method.
Other possible error messages for various JSON parse errors include:
SyntaxError: JSON.parse: unterminated string literal
SyntaxError: JSON.parse: bad control character in string literal
SyntaxError: JSON.parse: bad character in string literal
SyntaxError: JSON.parse: bad Unicode escape
SyntaxError: JSON.parse: bad escape character
SyntaxError: JSON.parse: unterminated string
SyntaxError: JSON.parse: no number after minus sign
SyntaxError: JSON.parse: unexpected non-digit
SyntaxError: JSON.parse: missing digits after decimal point
SyntaxError: JSON.parse: unterminated fractional number
SyntaxError: JSON.parse: missing digits after exponent indicator
SyntaxError: JSON.parse: missing digits after exponent sign
SyntaxError: JSON.parse: exponent part is missing a number
SyntaxError: JSON.parse: unexpected end of data
SyntaxError: JSON.parse: unexpected keyword
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: end of data while reading object contents
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: end of data when ',' or ']' was expected
SyntaxError: JSON.parse: expected ',' or ']' after array element
SyntaxError: JSON.parse: end of data when property name was expected
SyntaxError: JSON.parse: expected double-quoted property name
SyntaxError: JSON.parse: end of data after property name when ':' was expected
SyntaxError: JSON.parse: expected ':' after property name in object
SyntaxError: JSON.parse: end of data after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal
SyntaxError: JSON.parse: property names must be double-quoted strings
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
SyntaxError: JSON.parse Error: Invalid character at position {0} (Edge)
For instance, we shouldn’t pass in a JSON string that has trailing commas:
JSON.parse('[1, 2, 3, 4,]');
JSON.parse('{"foo": 1,}');
The first line has a trailing comma at the end of the array.
The 2nd has a trailing commas at the end of the object.
Instead, we should fix the error by removing them:
JSON.parse('[1, 2, 3, 4]');
JSON.parse('{"foo": 1}');
Also, property names must be double quoted strings, so instead of writing:
JSON.parse("{'foo': 1}");
We write:
JSON.parse('{"foo": 1}');
Also, we can’t have numbers with leading zeroes in our JSON string:
JSON.parse('{"foo": 01}');
Instead, we fix that by removing the leading zero:
JSON.parse('{"foo": 1}');
Trailing decimal points are also invalid JSON, so we can’t write:
JSON.parse('{"foo": 1.}');
Instead, we write:
JSON.parse('{"foo": 1.0}');
Conclusion
To fix the ‘SyntaxError: JSON.parse: bad parsing’ when we’re developing JavaScript apps, we should make sure we pass in a valid JSON string as an argument of the JSON.parse
method.