Concepts

toJSON serialization hook

Try the free JavaScript compiler

If callable, an object toJSON method is invoked before the replacer; its result is serialized instead of the original value. A Date supplies its ISO string or null.

Formal Syntax & Structure

javascript syntaxjavascript
object.toJSON(key)

Parameters & Attributes

NameTypeDescription
keyStringContaining property name or root empty key.

Return Value

Any

Replacement value.

Working Example

Example Snippetjavascript
const o={n:4,toJSON(){return this.n;}};console.log(JSON.stringify(o));
JavaScript Reference