CKTIS / JavaScript
JSON.parse: JSON text to a JavaScript value
JSON.parse reads text in the strict JSON grammar and returns its JavaScript value, while an optional reviver can transform the result from leaves to root.
Version: ECMAScript 5 onward; reviver context.source from ECMAScript 2026; example target: Node.js 24.22.2
Syntax
JSON.parse(text[, reviver])JSON.parse(text, reviver?) first converts text to a string, then recognizes exactly one JSON value: an object, array, string, number, true, false, or null. JSON requires double quotes and permits no comments, trailing commas, undefined, NaN, or Infinity. A "__proto__" key becomes an ordinary own data property; parsing does not change the object prototype.
If reviver is a function, the engine visits properties from children to parent; this is the holder of the current property and the root uses the empty key "". Returning undefined deletes a property, while another result replaces it. Since ECMAScript 2026, a third reviver argument is context; for an unmodified primitive, context.source contains the exact input slice, allowing a large integer to be recovered as BigInt despite its earlier rounding to Number.
Invalid syntax throws SyntaxError; an exception thrown by the reviver propagates. JSON.parse does not execute the text as code, but data from an untrusted source still needs validation against the expected schema before use. The method has been standard since ECMAScript 5, with context.source since ECMAScript 2026.
Examples
const value = JSON.parse(
'{"created":"2024-01-02T03:04:05.006Z","count":9007199254740993}',
function (key, current, context) {
if (key === "created") return new Date(current);
if (key === "count") return BigInt(context.source);
return current;
},
);
console.log(value.created instanceof Date, value.created.toISOString());
console.log(String(value.count));
const order = [];
JSON.parse('{"a":{"b":1}}', (key, current) => {
order.push(key || "<root>");
return current;
});
console.log(order.join(","));
try {
JSON.parse('{"trailing":true,}');
} catch (error) {
console.log(error instanceof SyntaxError);
}Result
true 2024-01-02T03:04:05.006Z 9007199254740993 b,a,<root> true