JSON guide · 2 min read
How to Debug JSON API Responses End to End
Debug API JSON problems from HTTP status and headers through raw body, syntax, schema and application-level validation.
Start with HTTP before touching JSON
A parser error can be caused by an authentication redirect, reverse-proxy error, 404 page or empty response. Record the exact request URL, method, status and Content-Type before assuming the JSON syntax is broken.
Inspect a safe prefix of the raw response
Reading raw text reveals whether the body is actually JSON, HTML or plain text. Redact tokens and personal data before logging or sharing any response content.
const response = await fetch(url);
const raw = await response.text();
console.log(response.status, response.headers.get('content-type'));
console.log(raw.slice(0, 300));Validate syntax without changing the evidence
Paste the raw body into a syntax validator before formatting or repairing it. Preserve the original payload so any repair can be compared with what the server actually returned.
Then validate the contract
A syntactically valid response can still violate the expected API shape. Check required fields, types and allowed values with JSON Schema, DTO validation or contract tests.
Compare good and bad responses structurally
If the endpoint worked previously, compare a known-good response with the failing one by JSON path. Structural comparison quickly reveals removed fields, changed types and unexpected nesting that line-based diffs can hide in formatting noise.
Turn the discovered failure into a regression test
Once the cause is understood, add a fixture or contract test for that response shape. Debugging effort is most valuable when it becomes a guardrail that prevents the same mismatch returning later.
- Transport and status test.
- Syntax parse test.
- Schema or DTO validation test.
- Edge-case fixture for the discovered failure.
Common questions
Frequently asked questions
Why does an API return HTML when I expect JSON?
Common causes include login redirects, error pages, wrong routes, proxy responses and framework fallbacks. Check status and Content-Type before parsing.
Should I log the entire failing API response?
Usually not. Log only the minimum redacted data needed for diagnosis because responses can contain credentials or personal information.
What if the JSON is valid but my application still fails?
Validate the expected schema, types and business assumptions. Syntax validity proves only that the text can be parsed as JSON.