Godot Master
extends Node
func _ready():
var dict = parse_json('{"D":1,"C":2,"B":3,"A":4}')
print(dict)
# Output : (A:4), (B:3), (C:2), (D:1)
Tested it with a few more letters, it seems to put the keys in alphabetical order.
Technically JSON has no obligation to keep the key order.
JSON, nor dictionaries, are designed to preserve ordering.
But Godot 3.0 Dictionaries do preserve order, so I was expecting it in this case too.
Oh, I didn't realize that. Still learning GDScript.
Seems the order is reversed. Solution may be as simple as changing a stack to a queue (etc.), which shouldn't have any perf. implications.
But, I would argue that you shouldn't design any application to rely on JSON key ordering.
But, I would argue that you shouldn't design any application to rely on JSON key ordering.
I agree.
Maybe we can add a note to the docs?
As explained by others above, JSON objects don't guarantee any order for their keys.
From RFC 7159, the JSON spec:
An object is an unordered collection
An array is an ordered sequence
Indeed, it works fine for arrays: parse_json('[1,2,3]') corretly yields [1, 2, 3].
So the only remaining thing is that if someone is familiar with Godot dicts, but unfamiliar with JSON objects, they might not expect this to happen. Will add a note to the docs.
Most helpful comment
Technically JSON has no obligation to keep the key order.