Why Google Chrome is changing order of JSON keys?

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?

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 Likes

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

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

1 Like

Why can’t I?

As Dormilich pointed out,[quote=“Dormilich, post:2, topic:301691”]
Note of caution: the JS specification explicitly states that there is no defined order of keys in an object
[/quote]

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,

^ _ ^

1 Like

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.

2 Likes