javascript - Is the order of elements in a JSON list preserved?

ID : 10406

viewed : 18

Tags : javascriptarraysjsonlistjavascript

Top 5 Answer for javascript - Is the order of elements in a JSON list preserved?

vote vote


Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine):

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

An array is an ordered sequence of zero or more values.

The terms "object" and "array" come from the conventions of JavaScript.

Some implementations do also preserve the order of JSON objects as well, but this is not guaranteed.

vote vote


The order of elements in an array ([]) is maintained. The order of elements (name:value pairs) in an "object" ({}) is not, and it's usual for them to be "jumbled", if not by the JSON formatter/parser itself then by the language-specific objects (Dictionary, NSDictionary, Hashtable, etc) that are used as an internal representation.

vote vote


Practically speaking, if the keys were of type NaN, the browser will not change the order.

The following script will output "One", "Two", "Three":

var foo={"3":"Three", "1":"One", "2":"Two"}; for(bar in foo) {     alert(foo[bar]); } 

Whereas the following script will output "Three", "One", "Two":

var foo={"@3":"Three", "@1":"One", "@2":"Two"}; for(bar in foo) {     alert(foo[bar]); } 
vote vote


Some JavaScript engines keep keys in insertion order. V8, for instance, keeps all keys in insertion order except for keys that can be parsed as unsigned 32-bit integers.

This means that if you run either of the following:

var animals = {}; animals['dog'] = true; animals['bear'] = true; animals['monkey'] = true; for (var animal in animals) {   if (animals.hasOwnProperty(animal)) {     $('<li>').text(animal).appendTo('#animals');   } } 
var animals = JSON.parse('{ "dog": true, "bear": true, "monkey": true }'); for (var animal in animals) {   $('<li>').text(animal).appendTo('#animals'); } 

You'll consistently get dog, bear, and monkey in that order, on Chrome, which uses V8. Node.js also uses V8. This will hold true even if you have thousands of items. YMMV with other JavaScript engines.

Demo here and here.

vote vote


"Is the order of elements in a JSON list maintained?" is not a good question. You need to ask "Is the order of elements in a JSON list maintained when doing [...] ?" As Felix King pointed out, JSON is a textual data format. It doesn't mutate without a reason. Do not confuse a JSON string with a (JavaScript) object.

You're probably talking about operations like JSON.stringify(JSON.parse(...)). Now the answer is: It depends on the implementation. 99%* of JSON parsers do not maintain the order of objects, and do maintain the order of arrays, but you might as well use JSON to store something like

{     "son": "David",     "daughter": "Julia",     "son": "Tom",     "daughter": "Clara" } 

and use a parser that maintains order of objects.

*probably even more :)

Top 3 video Explaining javascript - Is the order of elements in a JSON list preserved?