Back to Tools

Fix "Unexpected token < in JSON" Error

Complete guide to fixing this common JSON error

The "Unexpected token < in JSON" error is one of the most frustrating JSON errors. It occurs when you're expecting JSON data, but instead receive HTML (usually an error page).

The < character is the start of HTML tags, which means your API or server returned an HTML error page instead of JSON. Learn how to fix this instantly using our free JSON Fixer tool.

What Does "Unexpected token < in JSON" Mean?

This error means JavaScript's JSON.parse() encountered an HTML tag (starting with <) when it expected JSON. This typically happens when:

  • API endpoint returns an HTML error page (404, 500, etc.)
  • Server redirects to a login page
  • API endpoint doesn't exist
  • Content-Type header is wrong (text/html instead of application/json)
  • Network error returns HTML error page

Common Causes

1. API Endpoint Doesn't Exist (404 Error)

The server returns an HTML 404 page instead of JSON when the endpoint is wrong.

<html>
  <head><title>404 Not Found</title></head>
  <body>Page not found</body>
</html>

2. Server Error (500 Error)

Server crashes return HTML error pages instead of JSON responses.

3. Authentication Required

API requires authentication and redirects to a login HTML page.

How to Fix "Unexpected token < in JSON"

1

Check the API Response

Before parsing, check what you actually received. Log the response to see if it's HTML or JSON.

fetch('/api/users')
  .then(response => {
    const contentType = response.headers.get('content-type');
    if (!contentType.includes('application/json')) {
      throw new Error('Response is not JSON');
    }
    return response.json();
  });
2

Verify API Endpoint

Ensure the API endpoint URL is correct. Test it in a browser or use curl to see what it returns.

3

Check Response Status

Always check HTTP status codes. Only parse JSON if status is 200-299.

fetch('/api/users')
  .then(response => {
    if (!response.ok) {
      throw new Error('API error: ' + response.status);
    }
    return response.json();
  });

Fix JSON Errors Instantly

If you have valid JSON that needs fixing, use our free JSON Fixer to repair syntax errors automatically.

Try JSON Fixer Now

FAQ

Why am I getting HTML instead of JSON?

Usually because the API endpoint is wrong, the server returned an error page, or authentication is required. Check the API URL and response status code.

How do I prevent this error?

Always check the Content-Type header and HTTP status code before parsing JSON. Only parse if status is 200-299 and Content-Type is application/json.