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.
stringifySafe(value, replacer?, space?, cycleReplacer?)
value
A JavaScript value, usually an object or array, to be converted.
replacer optional function(key, value)
A function that transforms the results.
space optional
A number or a string to add indentation to the returned JSON string. If a number, indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). If a string, indicates the string used as white space (a maximum of 10 characters will be used).
cycleReplacer optional function(key, value)
A function that transforms circular references.
A string representing the value.
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]"}`