Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide certain values in output from JSON.stringify()

Is it possible to exclude certain fields from being included in the json string?

Here is some pseudo code

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

I want to exclude privateProperty1 and privateproperty2 from appearing in the json string

So I thought, I can use the stringify replacer function

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

and in the stringify

var jsonString = json.stringify(x,replacer); 

But in the jsonString I still see it as

{...privateProperty1:value..., privateProperty2:value } 

I would like to the string without the privateproperties in them.

like image 686
Nilesh Avatar asked Feb 06 '11 00:02

Nilesh


People also ask

How do I hide something in JSON?

To hide, remove or omit certain values from the output of the JSON. stringify() method, we can pass a replacer function as a second argument to the method in JavaScript.

What does JSON Stringify () method do?

stringify() The JSON. stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Is it bad to use JSON Stringify?

It`s ok to use it with some primitives like Numbers, Strings or Booleans. As you can see, you can just lose unsupported some data when copying your object in such a way. Moreover, JavaScript won`t even warn you about that, because calling JSON. stringify() with such data types does not throw any error.

How do I remove quotes from JSON Stringify?

That makes the regex to remove the quotes from the keys MUCH easier. Start your solution with this: var cleaned = JSON. stringify(x, null, 2);


1 Answers

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.

like image 75
Jared Farrish Avatar answered Sep 19 '22 11:09

Jared Farrish