sort keys with JSON.stringify()

# Felipe Gasper (14 years ago)

Hello,

Is it possible to get sorted keys:

{a:1,c:3}

…from running JSON.stringify when declaring an object thus:

{c:3,a:1}

??

# Felipe Gasper (14 years ago)

But even that is implementation-specific, right?

Object key order can be completely random…at least, that’s always been my understanding.

# Brian Kardell (14 years ago)

If you have non-numeric keys they should be in insertion order. Search the archives, this is a much discussed topic.

# David Bruant (14 years ago)

Key orders is supposed to follow the order used in Object.keys (step 6 after "The abstract operation /JO/(/value/) serializes an object.") which is the order of for-in loops which is supposed to be implementation specific.

However, engines have, in practice, followed more or less the same way of ordering keys which means that there would be some de-facto standard that the commitee is discussing to standardize (as far as I know). See strawman:enumeration (and discussion at the end)

David

Le 11/09/2011 04:14, Felipe Gasper a écrit :

# Douglas Crockford (14 years ago)

On 11:59 AM, Felipe Gasper wrote:

Is it possible to get sorted keys:

{a:1,c:3}

…from running JSON.stringify when declaring an object thus:

{c:3,a:1}

JSON objects are unordered. If order is important, then use an array.

# Felipe Gasper (14 years ago)

On 9/11/11 5:05 PM, Douglas Crockford wrote:

On 11:59 AM, Felipe Gasper wrote:

Is it possible to get sorted keys:

{a:1,c:3}

…from running JSON.stringify when declaring an object thus:

{c:3,a:1} JSON objects are unordered. If order is important, then use an array.

That’s actually my point.

What I would like is a way to have two objects with identical data serialize the same way--ideally (but, ok, not necessarily) with keys sorted alphabetically.

The most immediately use for such a feature is to deep-compare two objects, but there are probably other viable applications.

There apparently is no way to do this using browsers’ native serializers since (as has been mentioned) they usually serialize according to for..in order, which in turn depends on insertion order. I want a serialization that is agnostic with to insertion order, that only depends on what’s in the object here and now.

Does any of the JavaScript-based serializers offer such a feature?

# Brian Kardell (14 years ago)

But Doug, just to clarify: You mean that the parsed objects create no predictable insertion order right? It is actually possible to use replacer to serialize keys in a particular order, and that might be enough for what he is looking for (if two objects serialized as the same string they would be equal).

Perhaps I am over simplifying his question or I misunderstand your response.

# Felipe Gasper (14 years ago)

On 9/12/11 10:07 AM, Brian Kardell wrote:

But Doug, just to clarify: You mean that the parsed objects create no predictable insertion order right? It is actually possible to use replacer to serialize keys in a particular order, and that might be enough for what he is looking for (if two objects serialized as the same string they would be equal).

Almost. Actually, I want the converse: if two objects are equal, they serialize the same way.

I noodled with the replacer function for a bit but wasn’t able to get something that seems to work for objects of arbitrary depth.

# Brian Kardell (14 years ago)

I think depth shouldn't be the problem. At the crux of is is that that technique uses iteration order which has a bit of a defacto standard that would "guarantee" that certain classes of object would serialize objects with the same data in the same way. It has problems on the fringes though...if your data contains numeric keys or prototypes, for example. I say guarantee in quotes because sometimes a browser picks one of these things that isn't written up and bucks tradition... Sometimes they are punished because too much breaks, but sometimes not.