About
JSON.stringify transform an object as a string with a JSON structure
Example
Basic Object to JSON
- A JSON string from a javascript object
// a variable
var humanObject = {
name : "Nicolas",
sex : "Unknown"
}
// we call the function
console.log(JSON.stringify(humanObject));
Set to JSON
How to transform a set to an array
let stringify = JSON.stringify(
this.root,
(key, value) => {
if (typeof value === 'object' && value instanceof Set) {
if (value.size > 0) {
return [...value];
} else {
// Don't include children if there is no value
return undefined;
}
}
return value;
},
' '
);
Exclude from JSON
By returning undefined from the replacer, you exclude any key of the JSON string.
let stringify = JSON.stringify(
object,
(key, value) => {
if (value instanceof Crawler) {
return undefined;
}
return value;
},
null,
' '
);