Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify is ignoring object properties

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"}

like image 818
frigon Avatar asked Dec 11 '13 05:12

frigon


People also ask

Can you JSON Stringify an object?

stringify() JSON stringification is the process of converting a Javascript object to a flat JSON string that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. stringify() , as the Javascript standard specifies.

Why JSON Stringify does not work on array?

The JSON array data type cannot have named keys on an array. When you pass a JavaScript array to JSON. stringify the named properties will be ignored. If you want named properties, use an Object, not an Array.

Does JSON Stringify nested objects?

stringify does not stringify nested arrays. Bookmark this question. Show activity on this post.

What does JSON Stringify () method do?

JSON.stringify() The JSON.stringify() method converts a JavaScript 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.


2 Answers

When an object has its own toJSON() implementation, JSON.stringify() uses the object returned from that method and stringifies that. kendo.data.Model defines it's own toJSON() method which only returns the properties defined on the model, which is why you aren't seeing other values (e.g. dirty, id, uid) in the string result.

"If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation."

Here's an alternative if you must have all properties on the object:

var model = kendo.data.Model.define({     id: "ID",     fields: {         "Name": { type: "string" }     } }); var obj = new model(); obj.set("Name","Johhny Foosball");     var newObj = $.extend({}, obj); delete newObj.toJSON; document.write(newObj.dirty); document.write(JSON.stringify(newObj)); 

..and the updated fiddle.

Basically I used jQuery.extend to clone the object then deleted the toJSON function. Use at your own risk! :)

like image 191
Kevin Babcock Avatar answered Sep 29 '22 17:09

Kevin Babcock


If you're working in NodeJS you can use:

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

instead of JSON.stringify

like image 40
Berty Avatar answered Sep 29 '22 18:09

Berty