Godot: parse_json: Document that JSON does not preserve order like Godot's dicts

Created on 26 Jan 2018  路  7Comments  路  Source: godotengine/godot

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)
documentation

Most helpful comment

Technically JSON has no obligation to keep the key order.

All 7 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mefihl picture mefihl  路  3Comments

nunodonato picture nunodonato  路  3Comments

timoschwarzer picture timoschwarzer  路  3Comments

SleepProgger picture SleepProgger  路  3Comments

bojidar-bg picture bojidar-bg  路  3Comments