Is the order of elements in a JSON list preserved? Is the order of elements in a JSON list preserved? arrays arrays

Is the order of elements in a JSON list preserved?


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/valuepairs, 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 ofJavaScript.

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


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.


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]);}