Blog post
JSON.parse() Complete Guide: Reviver Function, Error Handling, and Edge Cases
Master JSON.parse() in JavaScript — reviver functions for date revival, error handling patterns, safe parsing wrappers, and common pitfalls to avoid.
Shashank Jain
Author


Article
What is JSON.parse()?
JSON.parse() parses a JSON string and returns the corresponding JavaScript value. It is the standard way to deserialize JSON from APIs, files, and storage.
Basic Usage
const obj = JSON.parse('{"name": "Alice", "age": 30}');
console.log(obj.name); // 'Alice'Always Wrap in Try/Catch
JSON.parse() throws a SyntaxError on invalid input. Never call it without error handling in production code.
function safeJsonParse(str, fallback = null) {
try {
return JSON.parse(str);
} catch {
return fallback;
}
}
// Usage
const data = safeJsonParse(responseText, {});The Reviver Function
The optional second argument to JSON.parse() is called for every key/value pair and lets you transform values during parsing.
// Auto-revive ISO date strings to Date objects
const data = JSON.parse(json, (key, value) => {
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(value)) {
return new Date(value);
}
return value;
});Common Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
| Unexpected token < | Server returned HTML (404/500) | Check res.ok before parsing |
| Unexpected token u | Input is undefined | Guard: if (str) JSON.parse(str) |
| Unexpected end of JSON | Truncated response | Check Content-Length / streaming |
Validate your JSON before parsing with the free JSON validator at jsondecode.com.
Keep reading
Recent blogs

May 22, 2026
JSONPath Guide: Query JSON Data Like SQL (with Examples)
Learn JSONPath syntax to query JSON data — basic expressions, wildcards, filters, recursive descent, and array slices. Includes JavaScript and Python examples.

May 22, 2026
How to Convert JSON to TypeScript Interfaces Automatically
Learn how to convert JSON to TypeScript interfaces — manually, with tools, and with AI. Includes nested objects, arrays, optional fields, and Zod schema generation.

May 22, 2026
JSON Schema Validation: A Complete Guide with Examples
Learn JSON Schema validation from scratch — Draft 7 syntax, required fields, type constraints, pattern matching, array validation, and validation with AJV in JavaScript.