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

Jun 14, 2026
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Serialize and deserialize JSON in C# using System.Text.Json and Newtonsoft.Json with practical examples.

Jun 14, 2026
JSON to Markdown Table: Convert JSON Arrays Instantly
Convert JSON arrays to Markdown tables in JavaScript, Python, and with the free online tool.

Jun 14, 2026
JSON in TypeScript: Type-Safe Parsing and Validation
Stop using any for JSON in TypeScript — use Zod, type guards, and generics for fully type-safe parsing.