Javascript Object - JSON.Stringify

1 - About

JSON.stringify transform an object as a string with a JSON structure

3 - Example

3.1 - 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)); 

3.2 - 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;
            },
            ' '
        );

3.3 - 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,
            ' '
        );

4 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap