API debugging guide

How to Debug an API JSON Response

When an API client says it “cannot parse JSON,” the JSON syntax may not be the real problem. Authentication redirects, HTML error pages, empty bodies, proxy failures and unexpected content types frequently surface as JSON parser exceptions.

Quick answer

Inspect the HTTP response before changing your parser: status code, final URL, Content-Type header and raw body usually reveal whether the server actually returned JSON.

1. Check status before parsing

A 401, 403, 404, 429 or 500 response may use a different body format than successful responses. Do not assume every status returns the same JSON shape.

2. Inspect Content-Type and raw body

If Content-Type is text/html and the body begins with <!doctype or <html>, fix the endpoint, authentication flow, proxy or server error instead of modifying JSON.parse.

3. Separate syntax from shape

Once the body is confirmed as JSON, validate its syntax. If it parses but your application still fails, compare the actual fields and types with the expected JSON Schema or TypeScript model.

Debugging checklist

HTTP status: 200?
Final URL: expected endpoint?
Content-Type: application/json?
Body empty?
Body starts with < ?
JSON syntax valid?
Expected fields and types present?

Related JSON tools and guides

Frequently asked questions

Why do I get Unexpected token < from an API?

The response is often HTML rather than JSON, commonly from a 404 page, login redirect, proxy error or framework error screen.

Should I parse every API response as JSON?

No. Check the status and content type, and handle empty or non-JSON responses intentionally.

What if the JSON is valid but my app still breaks?

Validate the data shape and types against the contract your application expects.

Source-backed reference

JSON standards quick reference

Concise, standards-backed facts for developers working with JSON debugging and validation. Each claim is linked to the evidence registry and resolves to a primary standard or platform reference so it can be independently verified.

JSON standard
JSON is a text format for serializing structured data, with syntax defined by interoperable standards rather than by FixTools.
Strings and object keys
JSON strings and object member names use double quotation marks. Single-quoted strings are not valid standard JSON syntax.
Trailing commas
Standard JSON grammar does not allow a comma after the final member of an object or the final element of an array.
Parser behavior
JSON.parse() parses JSON text into a JavaScript value and raises a SyntaxError when the input is not valid JSON.

Common invalid → valid JSON examples

Trailing comma

{"a":1,}{"a":1}

Single-quoted key

{'a':1}{"a":1}

Unsupported literal

{"score":NaN}{"score":null}

Built for verification, not just extraction

Direct answers are paired with source-specific evidence. This keeps technical claims independently verifiable and gives search and answer systems a clearer provenance trail than unsupported summary copy.