JSON.stringifySafe

A non-standard variant of JSON.stringify.

Converts a JavaScript value to a JSON (JavaScript Object Notation) string, but handles circular references by replacing them with the string "(circular)" or the return value of the provided cycle replacer.

Syntax

stringifySafe(value, replacer?, space?, cycleReplacer?)

Parameters

Return value

A string representing the value.

Examples

let x = {}
x.a = x
JSON.stringifySafe(x)
`{"a":"(circular)"}`
JSON.stringifySafe({ temperature: 20, humidity: 60, feel: "ok" })
`{"temperature":20,"humidity":60,"feel":"ok"}`
let x = {}
x.a = x
JSON.stringifySafe(x, null, null, (k, v) => `[circular reference in ${k}]`)
`{"a":"[circular reference in a]"}`

See also