JSON.parse() with reviver
ES5+Uses a reviver function to transform values during JSON parsing.
Syntax
JSON.parse(text, reviver)Parameters
text string The string to parse as JSON
reviver Function A function that receives key and value, and returns the transformed value
Return Value
any
The transformed JavaScript value
Examples
JavaScript
const json = '{"created":"2024-01-15","count":"42"}';
const obj = JSON.parse(json, (key, value) => {
if (key === 'created') return new Date(value);
if (key === 'count') return parseInt(value, 10);
return value;
});
console.log(obj.created instanceof Date);
console.log(typeof obj.count); Output:
// true
number