2

I am using kendo UI, but i think this is a general question.

Basically I am logging a object the result looks like this,

enter image description here

Then I JSON.stringify(obj), i get the output like this

{"ProductID":5,"ProductName":"Cream","UnitPrice":12,"Discontinued":false,"UnitsInStock":15,"Priority":5}

Question , can someone illustrate why the parameters "dirty",'uid' does not get stringified ? It would be great if you could actually post some example creating such objects.

FYI: my actual object is like the output of stringify which is passed to a Kendo Grid and i get the dirty object from one of the kendo Grid methods (basically gives the set of rows thats been edited in the grid)

What is think its something to do with the object.prototype. Maybe the parent properties does not get stringified ...

2
  • Just something i noticed, the stringify seems to stop once it gets to thte _events and _handlers properties. which are objects, not like values that came before them. Commented Nov 17, 2015 at 8:06
  • Can we see your code for where you create your object, and since we're at it, the line where you stringify it? Commented Nov 17, 2015 at 8:10

1 Answer 1

9

JSON.stringify only includes the object's own, enumerable properties whose names are strings. So there are three ways for properties to be left out: If they're inherited, if they're non-enumerable (such as those Object.defineProperty creates by default), or if their names aren't strings (ES2015 has the ability for properties to have Symbol names rather than string names).

This demonstrates two of those, the "own" and "enumerable" aspects: It logs {"answer":42}, for instance, because obj only has one own, enumerable property, answer. prop is inherited, and foo is non-enumerable:

// An object to use as a prototype
var proto = {
  prop: 1
};

// Create an object using that as its prototype
var obj = Object.create(proto);

// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
  value: 27
});

// Define an own, enumerable property
obj.answer = 42;

// Show the JSON for the object
snippet.log(JSON.stringify(obj));

This is in the specification for JSON.stringify:

Live Example:

// An object to use as a prototype
var proto = {
    prop: 1
};

// Create an object using that as its prototype
var obj = Object.create(proto);

// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
    value: 27
});

// Define an own, enumerable property
obj.answer = 42;

// Show the JSON for the object
snippet.log(JSON.stringify(obj));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Just for completeness, this demonstrates Symbol-named properties being left out as well (live copy on Babel's REPL):

// An object to use as a prototype
var proto = {
  prop: 1
};

// Create an object using that as its prototype
var obj = Object.create(proto);

// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
  value: 27
});

// Define an own, enumerable property with a string name
obj.answer = 42;

// Define an own, enumerable property with a Symbol name
var s1 = Symbol();
obj[s1] = "I'm enumerable and have a Symbol name";

// Show the JSON for the object
console.log(JSON.stringify(obj)); // {"answer":42}

// Proof that the Symbol property was enumerable comes
// from Object.assign
var obj2 = Object.assign(obj);
console.log(obj2[s1]); // I'm enumerable and have a Symbol name
Sign up to request clarification or add additional context in comments.

4 Comments

Amazing!. Did not know about the JSON.stringify only includes the object's own, enumerable properties thing.
I didn't either, kudos for knowing/finding that, Though it still may not be the case, I can't think of anything else unless we see the poster's code though
@T.J Crowder Interesting stuff , I have to play around with Object prototype more
@Sean_A91 I don't know either , I'm just calling a function of kendo UI, probably its doing something like this internally cause i did not add the other properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.