Concepts
toJSON serialization hook
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
| Name | Type | Description |
|---|---|---|
| key | String | Containing 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));