Why Google Chrome is changing order of JSON keys?


#1

I have JSON which is logged in Web Inspector Console, which is when collapsed looks something like this:

You can see key pair values something like video-2eb... and quiz-e68..., this is actual format in which it was encoded and logged into console.

But when expand the JSON object the video and quiz pairs reorder themselves to something like below:

Is it sorting key's in alphabetical manner?


#2

Likely, but that's just for display. It doesn't mean the keys in the underlying object are sorted as well.

Note of caution: the JS specification explicitly states that there is no defined order of keys in an object, so you should not rely on it.


#3

Thanks, but JSON objects remain in the order the way they were encoded right?


#4

No, you should not rely on the order of JSON keys. If you need to retain order, you need to use an Array.


#5

Why can't I?


#6

As Dormilich pointed out,

So it cannot be reliable as far as order is concerned. An array, however, does not change order unless you command it to do so.

V/r,

^ _ ^


#7

#8

Because the order is not guaranteed and your app will break.

Here is further reading on this subject:

If you would like help converting to use an Array, where ordered is guaranteed, then please create a new thread. But as it stands, you have asked the same question 3 times in a row and received 3 different answers that all say the same thing. Because of that, I do not feel anything else can come of this thread and I'm closing it.