Javascript Object - JSON.Stringify

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

Documentation / Reference





Discover More
Json - Library

Library or software that can parse Json file Language Name Description Stream, File, Console jq a command-line JSON processor. Java Org.Json org/json/JSONTokenerorg.json Parses a JSON (4627RFC...
Browser
Puppeteer - How to pass back and forth a date (or a complex type) to the headless browser via the evaluate function

A step by step guide that shows how to serialize and deserialize an object with a date ( ) when using the puppeteer evaluate...



Share this page:
Follow us:
Task Runner