Why Validate API Responses Before Parsing?
Even well-known APIs occasionally return malformed JSON due to server errors, encoding issues, or content negotiation failures. Validating before calling JSON.parse() prevents runtime crashes in production.
Common API JSON Issues Detected
- HTML error pages returned as JSON (Content-Type mismatch)
- Trailing commas from server-side template rendering
- Non-UTF-8 encoding or BOM characters at start of response
- Partial payloads from network timeouts or truncated responses
- NaN or Infinity values from numeric computation bugs
Who this guide is for
- Validate API responses are strict RFC 8259 JSON before parsing
- Catch malformed responses that would crash JSON.parse() in production
- Verify third-party API responses during integration testing
- Check webhooks payloads before writing handler logic