Ask any question about JavaScript here... and get an instant response.
Why am I getting a `SyntaxError: Unexpected token` when parsing JSON from a fetch response?
Asked on Nov 20, 2025
Answer
This error typically occurs when the response from a fetch request is not valid JSON. This can happen if the response is empty, malformed, or if you try to parse it before ensuring it's in the correct format. Here's how you can handle JSON parsing safely:
<!-- BEGIN COPY / PASTE -->
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- Ensure the URL you are fetching from returns a valid JSON response.
- Check the "response.ok" property to handle HTTP errors before parsing.
- Use "response.json()" to parse the response only if it's confirmed to be JSON.
- Handle errors using ".catch" to catch any parsing or network errors.
Recommended Links:
