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/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.
Can I Rely on the Order of a JSON Array?
Yes, you can! Arrays are made, so that order matters! That is what divides Objects from Arrays. Especially in JS and JSON.
JSON array deserialization -- is the order of elements always preserved?
Yes.
The JSON spec does specify that the order is preserved for arrays as they are an ordered collection of values: JSON.org
Preserve javascript key order when they're integers
In JSON, by definition, the name/value pairs in an object {}
are unordered: They have no specific order in which they appear. In a list []
on the other hand, the order is preserved.
See also this other StackOverflow question where the answer cites the official documentation.
As @Bergi already pointed out in the comments, putting products into a list is the proper solution.
JSON.parse('{"products": [ {"key": 0, "text": "Please Select"}, {"key": 2, "text": "Example 1"}, ... ]}');
for (let product of res.products) {
$('#myList').append('<option value="'+product.key+'">'+product.text+'</option>');
}
Does json.Unmarshal maintain order of JSON arrays
Yes, ordering is the fundamental property of array data structures, so go implicitly maintains it when un/marshaling slices and arrays to and from JSON.
Note however that Associative arrays, called "Objects" in JSON and "Map types" in go (or simply "maps"), are "an unordered group of elements", so un/marshaling them to and from JSON does not preserve the order of the key/value pairs.
Finally, marshaling a struct into a JSON object (aka an associative array) does maintain the order of keys based on the order of struct fields, per json.Marshal(...)
.
Keep the order of the JSON keys during JSON conversion to CSV
Solved.
I used the JSON.simple library from here https://code.google.com/p/json-simple/ to read the JSON string to keep the order of keys and use JavaCSV library from here http://sourceforge.net/projects/javacsv/ to convert to CSV format.
Does JSON.stringify preserves order of objects in array
There is nothing in the docs that explicitly confirms that the order of array items is preserved. However, the docs state that for non-array properties, order is not guaranteed:
Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
Even if the order of array items would be preserved, I would not count on this but rather sort the items myself. After all, there will most likely be some business or presentation logic that indicates how the items should be sorted.
Java JSON object insertion order maintenance
Arrays are ordered so use an array of key-value objects [ {key1: val1}, {key2: val2} ]
Related Topics
How to Escape Regular Expression Special Characters Using JavaScript
Issue in Returning Data Retrieved from Db Queries Called in the Loop
Browsers, Time Zones, Chrome 67 Error (Historic Timezone Changes)
Javascript/Jquery: $(Window).Resize How to Fire After the Resize Is Completed
Chrome Desktop Notification Example
How to Dynamically Change Header Based on Angularjs Partial View
Object.Watch() for All Browsers
Replace Multiple Characters in One Replace Call
Center a Popup Window on Screen
How to Listen to a "Style Change" Event
Facebook Social Plug-In Not Showing Up When Added Dynamically
How to Pass Data from a Page to Another Page Using React Router
How to Replace Innerhtml of a Div Using Jquery
Are There Constants in JavaScript
Differencebetween Screenx/Y, Clientx/Y and Pagex/Y
Using Rails 3.1, Where Do You Put Your "Page Specific" JavaScript Code