Hide specific values ​​on output from JSON.stringify ()

Is it possible to exclude certain fields from json string?

Here are some pseudo codes

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

I want privateProperty1 and privateproperty2 to appear in json string

So, I thought, I can use the replicate function for stringify

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

and in stringify

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

But in jsonString, I still see it as

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

I need a string without privateproperties in them.

+74
json javascript
Feb 06 2018-11-11T00:
source share
10 answers

Mozilla docs say they 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 way to duplicate if you decide to go this route (according to 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 avoid confusion.

+82
Feb 06 2018-11-11T00:
source share

Another good solution: (emphasis required)

 x.toJSON = function () { return _.omit(this, [ "privateProperty1", "privateProperty2" ]); }; 

The advantage of this solution is that anyone calling JSON.stringify on x will have the correct results - you do not need to individually change the JSON.stringify calls.

Version without underline:

 x.toJSON = function () { var result = {}; for (var x in this) { if (x !== "privateProperty1" && x !== "privateProperty2") { result[x] = this[x]; } } return result; }; 
+21
Feb 01 '16 at 19:10
source share

You can use your own defineProperty function from Object:

 var data = {a: 10}; Object.defineProperty(data, 'transient', {value: 'static', writable: true}); data.transient = 'dasda'; console.log(JSON.stringify(data)); //{"a":10} 
+14
Jan 20 '16 at 2:38
source share

An easy way to do it.

  • Create a variable and assign an empty array. This makes the object a prototype of the array.
  • Add non-digital keys to this object.
  • Serialize this object using JSON.stringify
  • You will see that nothing is serialized from this object.

~~~

 var myobject={ a:10, b:[] }; myobject.b.hidden1 = 'hiddenValue1'; myobject.b.hidden2 = 'hiddenValue2'; //output of stringify //{ // "a": 10, // "b": [] //} 

~~~

http://www.markandey.com/2015/07/how-to-hide-few-keys-from-being-being.html

+3
Jul 19 '15 at 10:00
source share

Object.create is another solution close to defineProperty (properties are defined in the same way), but in this way you define properties for expansion from the very beginning. Thus, you can set only those properties that you want by setting the value of the enumerable property to true (false by default), JSON.stringify ignores non-enumerable properties, the disadvantage is that this property will also be hidden when using for-in on the object or functions like Object.keys.

 var x = Object.create(null, { x: {value:0, enumerable: true}, y:{value: 0, enumerable: true}, divID: {value: 'xyz', enumerable: true}, privateProperty1: {value: 'foo'}, privateProperty2: {value: 'bar'} }); JSON.stringify(x) //"{"x":0,"y":0,"divID":"xyz"}" 
+2
Sep 10 '16 at 15:27
source share

Note to Miroslav Dialag answer : A specific property must be its property. Otherwise, it will fail.

Does not work:

 class Foo { } Object.defineProperty(Foo.prototype, 'bar', { value: 'bar', writable: true }); const foo = new Foo(); foo.bar = 'baz'; alert(JSON.stringify(foo).indexOf('bar') === -1); // false (found) 

Works:

 class Foo { constructor() { Object.defineProperty(this, 'bar', { value: 'bar', writable: true }); } } const foo = new Foo(); foo.bar = 'baz'; alert(JSON.stringify(foo).indexOf('bar') === -1); // true (not found) 
+1
Aug 30 '17 at 10:52 on
source share

I know that this is already the answer to the question, but I would like to add something when using objects with installation.

If you assigned it using a function, it will not be included in the result of JSON.stringify ().

To access a value, call it as a function, ending with a ()

 var MyClass = function(){ this.visibleProperty1 = "sample1"; this.hiddenProperty1 = function(){ return "sample2" }; } MyClass.prototype.assignAnother = function(){ this.visibleProperty2 = "sample3"; this.visibleProperty3 = "sample4"; this.hiddenProperty2 = function(){ return "sample5" }; } var newObj = new MyClass(); console.log( JSON.stringify(newObj) ); // {"visibleProperty1":"sample1"} newObj.assignAnother(); console.log( JSON.stringify(newObj) ); // {"visibleProperty1":"sample1","visibleProperty2":"sample3","visibleProperty3":"sample4"} console.log( newObj.visibleProperty2 ); // sample3 console.log( newObj.hiddenProperty1() ); // sample2 console.log( newObj.hiddenProperty2() ); // sample5 

You can also play with the concept, even if not on objects with installed objects.

0
Jun 18 '15 at 3:17
source share
 abstract class Hideable { public hidden = []; public toJSON() { var result = {}; for (var x in this) { if(x == "hidden") continue; if (this.hidden.indexOf(x) === -1) { result[x] = this[x]; } } return result; }; } 
0
Mar 18 '18 at 17:23
source share

You can do it easily with ES2017

 let {privateProperty1:exc1, privateProperty2:exc2, ...foo} = { x:0, y:0, divID:"xyz", privateProperty1: 'foo', privateProperty2: 'bar' } 

Here privateProperty1 and privateProperty2 are assigned to exc1 and exc2 respectively. Residues are assigned to the newly created variable foo

0
Jul 17 '18 at 23:25
source share

I used the toJSON solution, based on a small library that I wrote to type text at runtime https://stackoverflow.com/a/464625/

0
Apr 30 '19 at 8:49
source share



All Articles