When building client-side apps, API requests often fail with cryptic "Unexpected token < in JSON at position 0" or unexpected character errors. Follow this step-by-step workflow to diagnose the failure without guessing.
The most common JSON error ("Unexpected token < at position 0") occurs when the API returns an HTML 404/500 error page starting with <!DOCTYPE html>. Log the raw HTTP status and response.text() first.
Paste the raw response body into the client-side JSON Validator. The AST parser will point to the exact row and column of the syntax violation.
If the JSON looks visually correct in your editor but JSON.parse() throws an unexpected token at position 0, test for a UTF-8 Byte Order Mark (\uFEFF) or zero-width spaces.
Format the corrected payload into clean 2-space indented structure to inspect nested fields and verify AST validity.
Test raw HTML error response strings in JSON Validator.
<!DOCTYPE html> <html><body>502 Bad Gateway</body></html>