JavaScript JSON.parse Errors
JavaScript JSON.parse converts a JSON string into a JavaScript value. It throws a SyntaxError when the input is not strict JSON, but the exact message varies across JavaScript engines and browsers.
Quick answer
Before calling JSON.parse, confirm you actually have a string containing JSON. Many parsing bugs come from HTML responses, empty strings, already-parsed objects or JavaScript object-literal syntax that is not valid JSON.
Unexpected token
Inspect the character mentioned by the error and the syntax immediately before it. Common causes are missing commas, single quotes, unquoted keys, trailing commas and parsing an HTML response.
Unexpected end of JSON input
The string is usually empty, truncated or missing a closing quote, brace or bracket. Log the string length and final characters before parsing.
Avoid double parsing
fetch(...).then(r => r.json()) already parses the JSON body. Calling JSON.parse on the resulting object is incorrect. Know whether each boundary returns text or an already-decoded value.
Safer parse debugging
try {
const value = JSON.parse(text);
} catch (error) {
console.error(error.message);
console.error(text.slice(0, 200));
}Related JSON tools and guides
Frequently asked questions
What does JSON.parse return?
It returns the JavaScript value represented by the JSON string: object, array, string, number, boolean or null.
Can JSON.parse read single quotes?
No. JSON strings and object property names require double quotes.
Does response.json() use JSON.parse?
Conceptually it parses the response body as JSON for you, so you normally should not JSON.parse the returned value again.