JSON properties order

Hi there,
I have a problem with ordering properties while JSON.stringifying object.
I have json file that looks like this:

	"id": 0,
	"scene_set": 2,
	"name": "SELECT_WORD",
	"content": [

When I JSON.parse it to object and try to JSON.stringify it, each time I get randomly mixed properties order:

	"scene_set": 2,
	"content": [
	"id": 0,
	"name": "SELECT_WORD"

Now, I know that Object doesn’t have ordered properties like in Array, but I’m wondering is there any way to JSON.parse with properties mapping so JSON.stringify result would be as source JSON file?

No there is no default way to do that.

From the sources avmplus/core/JSON.as

you can read in the comments

Property enumeration order is indeterminate in AS3, so there is no attempt to extract
keys in any particular order when enumerating properties during filtering.

also from the documentation of for…in

Object properties are not kept in any particular order, so properties may appear in a seemingly random order.

You will have to come up with a sort function for your object, and/or probably a way to do it with the Proxy class


dynamic class SortedObject extends Proxy
    override flash_proxy function nextNameIndex( index:int ):int
        // .. sort the property names here

a simple way to sort is to use for..in to iterates through all the properties of the object, save them into an Array, sort the array based on the properties name, then reassemble into an Object

It crossed my mind to build Array with sorted properties, but the problem is in parsing external json file created by “third party” and I need to sort properties as they are arranged in string.

I tried using reviving function with JSON.parse method, but it throws properties not ordered as arranged in string.

As I understand, JSON class should first split string to atoms and then build object properties from it, so overriding should be done in JSON class rather than overriding flash_proxy function. Am I right? If yes, how can I do it?

BTW, I tried to do it with JavaScript and it works just fine, so I could just find it as a lack in JSON class.

the rule is like it says in the doc “order is indeterminate”

no it is not a lack in the jSON part, refer to
RFC7159 - The JavaScript Object Notation (JSON) Data Interchange Format

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.


JSON parsing libraries have been observed to differ as to whether or
not they make the ordering of object members visible to calling
software. Implementations whose behavior does not depend on member
ordering will be interoperable in the sense that they will not be
affected by these differences.

in short it depends on the engine behind it, V8 engine will work a certain way, other JS engines will work maybe or not differently

anyway, the goal of JSON is data interchange not data ordering

you would need to “modify in place”, eg. follow whatever order is determined by a 3rd party
reviving function will not help

to do that you would have to build your own JSON parser instead of using the one by default in AS3

If you use com.adobe.serialization.json.JSON class it parses the keys in order when you decode.

sources here

Perfect! Thank you all!
Now I can create an array of properties order and use it when stringifying object.
Exactly what I was looking for!