JSON.stringify is ignoring object properties

Viewed 17275

See the jsfiddle example http://jsfiddle.net/frigon/H6ssq/

For some reason there are fields that JSON.stringify is ignoring. Is there a way to force JSON.stringify to parse them?

As the jsfiddle shows... this code...

<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
    <script>
    var model = kendo.data.Model.define({id: "ID", fields: {"Name":{type: "string"}}});
    var obj = new model();
    obj.set("Name","Johhny Foosball");
    document.write("<br />obj.dirty property exists: ");
    document.write(obj.dirty);
    document.write("<br/>obj.uid property exists: ");
    document.write(obj.uid);
    document.write("<br/>But they dont show in JSON.stringify():<br/>");    
    document.write(JSON.stringify(obj));
</script>

will output:

obj.dirty property exists: true

obj.uid property exists: b4af4dfc-9d94-4a2d-b286-d6f4cbc991d8

But they dont show in JSON.stringify():

{"ID":"","Name":"Johhny Foosball"}

4 Answers

If you're working in NodeJS you can use:

import * as util from 'util';
util.inspect(obj)

instead of JSON.stringify

It is using its own JSON.stringify method. This has happened to me with Sequelize. I stringify the object before adding the new property like this:

objStringified = JSON.stringify(obj) // uses its own JSON.stringify method
obj = JSON.parse(objStringified) // only shows properties specified as columns in its model
obj.dirty = "whatever";
finalObj = JSON.stringify(objParsed); // now it doesn't have its own JSON.stringify method, so dirty property appears
Related