The Mozilla docs say to return undefined (instead of "none"):

http://jsfiddle.net/userdude/rZ5Px/

function replacer(key,value)
{
    if (key=="privateProperty1") return undefined;
    else if (key=="privateProperty2") return undefined;
    else return value;
}

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
};

alert(JSON.stringify(x, replacer));

Here is a duplication method, in case you decide to go that route (as per your comment).

http://jsfiddle.net/userdude/644sJ/

function omitKeys(obj, keys)
{
    var dup = {};
    for (var key in obj) {
        if (keys.indexOf(key) == -1) {
            dup[key] = obj[key];
        }
    }
    return dup;
}

var x = {
    x:0,
    y:0,
    divID:"xyz",
    privateProperty1: 'foo',
    privateProperty2: 'bar'
};

alert(JSON.stringify(omitKeys(x, ['privateProperty1','privateProperty2'])));

EDIT - I changed the function key in the bottom function to keep it from being confusing.

Answer from Jared Farrish on Stack Overflow
Discussions

How can I remove escape sequences from JSON.stringify so that it's human-readable?
Space: A String or Number object that's used to insert white space into the output JSON string for readability purposes. Replacer: A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting the properties ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to remove nested JSON.stringify() properties
Anything else gets undefined, which means it gets omitted from the result. ... You can use a replacer function for this. var obj = { "id": 1, "name": "Light Switch", "lightStatus": true, "inputPort": { "id": 2, "value": 0 }, "outputPort": { "id": 2, "value": false }, "resistance": 100 }; var stringified = JSON... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 1, 2017
How to remove special characters from json.stringify
You need to get e-mail from params something like this: const emailValue = route.params.email; You can even destructure the e-mail: const { email } = route.params More on reddit.com
๐ŸŒ r/reactnative
15
3
July 13, 2021
How to remove \n after JSON.stringfy?
I parse the data from website and try to change to a json object. ... Copyfunction outPutJSON() { for (var i = 0; i < movieTitle.length; i++) { var handleN = movieContent[i]; console.log('===\n'); console.log(handleN); data.movie.push({ mpvieTitle: movieTitle[i], movieEnTitle: movieEnTitle[i], theDate: theDate[i], theLength: theLength[i], movieVersion: movieVersion[i], youtubeId: twoId[i], content: movieContent[i] }); }; return JSON.stringify... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Melvin George
melvingeorge.me โ€บ blog โ€บ hide-remove-omit-certain-values-keys-from-json-stringify-output-javascript
How to hide, remove or omit certain values or keys from the JSON.stringify() method's output in JavaScript? | MELVIN GEORGE
June 13, 2021 - // a simple object const personDetails = { name: "John Doe", age: 23, pin: 686612, mob: 9445678654, }; // Stringify personDetails object // using JSON.stringify() method const personDeatilsStr = JSON.stringify(personDetails, (key, value) => { // Check if key matches the string "pin" or "mob" // if matched return value "undefined" if (key === "pin" || key === "mob") { return undefined; } // else return the value itself return value; }); console.log(personDeatilsStr); /* OUTPUT ------ { "name":"John Doe", "age":23 } */ Now if we look at the output, we can see that the key pin and mob are removed from the output string.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ tryit.asp
JSON.stringify() will remove any functions from an object.
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-remove-a-json-attribute
JavaScript | Remove a JSON attribute | GeeksforGeeks
August 11, 2023 - In this article, we will see how to remove a JSON attribute from the JSON object. To do this, there are few of the mostly used techniques discussed.
Find elsewhere
Top answer
1 of 16
179

This simple regular expression solution works to unquote JSON property names in most cases:

const object = { name: 'John Smith' };
const json = JSON.stringify(object);  // {"name":"John Smith"}
console.log(json);
const unquoted = json.replace(/"([^"]+)":/g, '$1:');
console.log(unquoted);  // {name:"John Smith"}

Extreme case:

var json = '{ "name": "J\\":ohn Smith" }'
json.replace(/\\"/g,"\uFFFF");  // U+ FFFF
json = json.replace(/"([^"]+)":/g, '$1:').replace(/\uFFFF/g, '\\\"');
// '{ name: "J\":ohn Smith" }'

Special thanks to Rob W for fixing it.

Limitations

In normal cases the aforementioned regexp will work, but mathematically it is impossible to describe the JSON format with a regular expression such that it will work in every single cases (counting the same number of curly brackets is impossible with regexp.) Therefore, I have create a new function to remove quotes by formally parsing the JSON string via native function and reserialize it:

function stringify(obj_from_json) {
    if (typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){
        // not an object, stringify using native function
        return JSON.stringify(obj_from_json);
    }
    // Implements recursive object serialization according to JSON spec
    // but without quotes around the keys.
    let props = Object
        .keys(obj_from_json)
        .map(key => `${key}:${stringify(obj_from_json[key])}`)
        .join(",");
    return `{${props}}`;
}

Example: https://jsfiddle.net/DerekL/mssybp3k/

2 of 16
34

It looks like this is a simple Object toString method that you are looking for.

In Node.js this is solved by using the util object and calling util.inspect(yourObject). This will give you all that you want. follow this link for more options including depth of the application of method. http://nodejs.org/api/util.html#util_util_inspect_object_options

So, what you are looking for is basically an object inspector not a JSON converter. JSON format specifies that all properties must be enclosed in double quotes. Hence there will not be JSON converters to do what you want as that is simply not a JSON format.Specs here: https://developer.mozilla.org/en-US/docs/Using_native_JSON

Object to string or inspection is what you need depending on the language of your server.

๐ŸŒ
DEV Community
dev.to โ€บ alwarg โ€บ purging-unwanted-properties-in-js-object-45ol
Purging unwanted properties in js object - DEV Community
June 10, 2020 - From the above object, i want to remove the properties which are having the following values ... { name: 'Alwar G', info: { personal: { family members: ['father', 'mother'], address: { street: '1st avenue', place: 'chennai' } }, business: { address: { street: '1st avenue', place: 'chennai' } } } } ... I got the answer. Let's see the below code ยท function getPurgedObj(obj){ let stringfiedObj = JSON.stringify(obj, (key, value) => { return ['', null].includes(value) || (typeof value === 'object' &&(value.length === 0 || Object.keys(value).length === 0)) ?
๐ŸŒ
Online String Tools
onlinestringtools.com โ€บ json-parse-string
JSON Unstringify a String โ€“ Online String Tools
In this example, we apply the JSON.parse() function to a quote by Robert Louis Stevenson. Note that the input string is already stringified (that is, it is placed in double quotes, and it uses the characters \n and \t instead of newlines and tabs).
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ removing properties from objects in javascript
Removing Properties from Objects in JavaScript | Sentry
Use the delete operator to remove a property from an object. let person = { firstName: "John", lastName: "Doe", gender: "Male", age: 34 }; // Delete the age property first delete person.age; let json = JSON.stringify(person); console.log(json); ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 76343691 โ€บ stringify-json-removing-id-and-values-properties
Stringify JSON removing "$id" and "$values" properties
Sanitize the '$values' property if (obj.hasOwnProperty('$id') && obj.hasOwnProperty('$values')) { return sanitizeJson(obj['$values']); } // Make a new object from the '$id' property and sanitize the remaining properties return Object.fromEntries( Object.entries(obj) .filter(([key]) => key !== '$id') .map(([key, value]) => [key, sanitizeJson(value)]) ); } return obj; } const json = '{"$id":"1","$values":[{"$id":"2","prop1":"result","prop2":"result","prop3":{"$id":"3","$values":[{"$id":"4","prop1":"result","prop2":null},{"$id":"4","prop1":"result","prop2":null}]}}]}'; const jsonObject = JSON.parse(json); const result = JSON.stringify(sanitizeJson(jsonObject), null, '\t'); console.log(result);
๐ŸŒ
Muffin Man
muffinman.io โ€บ blog โ€บ json-stringify-removes-undefined
JSON.stringify removes undefined, how to keep it ยท Muffin Man
October 1, 2018 - This is something I keep rediscovering, because I keep forgetting it. JSON.stringify will omit all object attributes that are undefined. In most cases, it doesn't really matter, because if we parse th...
๐ŸŒ
GitHub
gist.github.com โ€บ Tanapruk โ€บ 4ab1505ead2491229235c886b6fbfaac
JSON.stringify remove slash double slash ยท GitHub
Simply remove " in the replacer function. When stringify that " won't get escape and /// won't show up.
๐ŸŒ
Online Tools
onlinetools.com โ€บ json โ€บ unstringify-json
Unstringify JSON โ€“ Online JSON Tools
Simple, free, and easy-to-use online tool that unstringifies JSON. Just upload your JSON string here and you'll instantly get unstringified JSON or JavaScript data.
๐ŸŒ
Muffinman
muffinman.io โ€บ json-stringify-removes-undefined
JSON.stringify removes undefined, how to keep it - Muffin Man
October 1, 2018 - You will be automatically redirected to https://muffinman.io/blog/json-stringify-removes-undefined/.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_stringify.asp
JavaScript JSON stringify() Method
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion ... var obj = { "name":"John", "age":30, "city":"New York"}; var myJSON = JSON.stringify(obj); document.getElementById("demo").innerHTML = myJSON; Try it Yourself ยป