8

I am creating a javascript object as follows

var myObjects ; 
for(var i = 0; i <10;i++){
    var eachObject = {"id" : i};
    myObjects .push(eachObject );
}
message = {
      "employeeDetails" : myObjects 
}

After this I stringify them as follows

JSON.stringify(message);

Does the above method stringifies the objects in the order they were previously? After stringify will they be in the order 0,1,2....9 as they were previously?

Arun Rahul
  • 527
  • 6
  • 22
  • What is keeping you from pulling up the JavaScript console in your web browser and trying this? – George Stocker Jun 16 '14 at 11:22
  • You could easily just try it out: `JSON.parse(JSON.stringify([0,1,2,3,4,5,6,7,8,9]))` – Cerbrus Jun 16 '14 at 11:22
  • The posted above is just dummy object . I am having huge array which contains more than 3000 objects and also I cannot say whether they are in particular order just by watching through eye as they cannot contain any index (a unique thing to identify its order) – Arun Rahul Jun 16 '14 at 11:38
  • It doesn't change anything, whether you work on large or dummy object. If test proposed above by _George Stocker_ or _Cerbrus_ gives you satisfying results, then you should achieve the same on your real object, no matter how long it it. – trejder Jun 16 '14 at 11:53
  • This question appears to be off-topic because it could be simply tested with a few lines of code. – dbugger Jun 16 '14 at 12:05
  • There is an answer that did the job for me here : https://stackoverflow.com/a/16543302/1579667 which says to use the second parameter of `JSON.stringify()` – Benj Aug 25 '17 at 14:42

3 Answers3

15

There is nothing in the docs that explicitly confirms that the order of array items is preserved. However, the docs state that for non-array properties, order is not guaranteed:

Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Even if the order of array items would be preserved, I would not count on this but rather sort the items myself. After all, there will most likely be some business or presentation logic that indicates how the items should be sorted.

Christophe Herreman
  • 15,366
  • 7
  • 55
  • 86
5

You can sort arrays using sort method.

And yes stringify retains ordering.

jsfiddle

var cars = ["Saab", "Volvo", "BMW"];
cars.push("ferrari");
alert(JSON.stringify(cars));
cars.sort();
alert("sorted cars" + JSON.stringify(cars));
NimChimpsky
  • 43,542
  • 55
  • 186
  • 295
  • 3
    In my experience, JSON.stringify sometimes DOES change the order of elements in array. – Psddp Dec 01 '16 at 15:57
  • It is sorting based on the length of the key. I am using chrome. I guess there is no way to sort array of objects based on keys. – Uzumaki Naruto Jun 19 '17 at 05:54
  • @UzumakiNaruto no, that is a coincidence. It is sorting by ASCII order. With all uppercase letters coming before lowercase letters. Try changing "ferrari" to just "ferr" or change "Volvo" to "Bolvo" to see the difference in behavior. – Philipp Hanes Oct 04 '17 at 18:08
5

For a simple way to get top level objects in a specific order from JSON.stringify() you can use:

const str = JSON.stringify(obj, Object.keys(obj).sort());

The order of the keys [] passed as the replacer determines the order in the resulting string.

See: https://dev.to/sidvishnoi/comment/gp71 and "sort object properties and JSON.stringify" - https://www.tfzx.net/article/499097.html

Another more rigorous approach is: https://github.com/develohpanda/json-order

And:https://www.npmjs.com/package/fast-json-stable-stringify will output in key order. You can also use your own sort order function.

I found this after lots of Google'ing.

nevf
  • 3,731
  • 6
  • 27
  • 31