0

I am facing an issue that JSON.stringify not stringifies all the keys in a JSON Object.

ie. window.performance.getEntries()[0] contains around 17 keys. But on converting to a string, the result contains only 4 keys.

How can I convert all the keys in window.performance.getEntries()[0]?

I want the complete string output of window.performance.getEntries() which is an array and I used JSON.stringify(window.performance.getEntries()).

Thanks in advance..

Saidh
  • 1,131
  • 1
  • 12
  • 21
  • sample for `window.performance.getEntries()[0]` – Grundy Nov 12 '15 at 13:34
  • Sorry, but without an example or screenshot, we cannot help you... – Mr. Polywhirl Nov 12 '15 at 13:36
  • Works for me `JSON.parse(JSON.stringify(window.performance.getEntries())).length == window.performance.getEntries().length == true` have you examined the entry at which parsing stops? – Alex K. Nov 12 '15 at 13:36
  • @AlexK. — `window.performance.getEntries()` != `window.performance.getEntries()[0]` The question is asking about the properties of the objects in the array that returns, not the entries in the array itself. – Quentin Nov 12 '15 at 13:37
  • Aye but *I want the complete string output of window.performance.getEntries()* – Alex K. Nov 12 '15 at 13:37
  • @AlexK. — Which would include **all** of the properties of all the objects it contains, not just **some** of the properties for all of the objects. – Quentin Nov 12 '15 at 13:38
  • That is very interesting that it is only including only four. For the lazy, run it in your Chrome console... – epascarello Nov 12 '15 at 13:39
  • For sample Just paste window.performance.getEntries() or Just performance.getEntries() in browser console. – Saidh Nov 12 '15 at 13:40
  • Possible duplicate of [JSON.stringify is ignoring object properties](http://stackoverflow.com/questions/20511421/json-stringify-is-ignoring-object-properties) – David P. Nov 12 '15 at 13:47

3 Answers3

1

window.performance seems to have is own toJSON-function and so can determine what will be stringified. Here is a answer and a work around to your question from a similiar question: https://stackoverflow.com/a/20511811/3400898

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

Community
  • 1
  • 1
David P.
  • 370
  • 1
  • 13
0

As other stated it is because there is a toJSON method defined. Basically you need to loop over every index of the array and than every property in the object.

var adjusted = window.performance.getEntries().map( function (result) {       
    var temp = {}, key;
    for (key in result) if (key!=="toJSON") temp[key]=result[key]; 
    return temp;
});
console.log(JSON.stringify(adjusted[0]));
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

The simplified solution for this problem which I found is

var jsonArray = $.map(performance.getEntries(),function(jsonObj){
    var obj = $.extend({},jsonObj);
    delete obj.toJSON;
    return obj;
});

JSON.stringify(jsonArray);
Saidh
  • 1,131
  • 1
  • 12
  • 21