Length of array in a dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Not_a_Robot
:warning: Old Version Published before Godot 3 was released.

Hello everyone!

I made a json (since this is the format I found that works with Godot) like this one:

{
"people" : [
            
            {"experts" : [
                            {"id" : 1, "groupId" : 1, "name" : "Json Carter", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} },
                            {"id" : 2, "groupId" : 2, "name" : "Jack Malorne", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} },
                            {"id" : 3, "groupId" : 3, "name" : "Igor the one", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} },
                            {"id" : 4, "groupId" : 4, "name" : "Anastasia Gold", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} },
                            {"id" : 5, "groupId" : 5, "name" : "Jessy James", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} },
                            {"id" : 6, "groupId" : 6, "name" : "James Jessy", "websites" : {"corporation": "godotengine.org", "other": "gitlab.com"} }

            ]}

]

}

How can I get the length (for example) of the experts array when loaded in a dictionary?

To be able to do loops to get all data the way I prefer.

Thank you in advance.

:bust_in_silhouette: Reply From: Zylann

When you have this data as a dictionary, you can get the length of experts this way:

data["people"][0]["experts"].size()
# or
data.people[0].experts.size()

Great! I’m extremely grateful, thank you!

As a side question… is it possible to remove the root key?
I modified that json a bit and I wonder if it is possible to do data[0][comment0-0] (just as an example).

I can’t because the engine expects a ‘{’ so I can’t start the file with a ‘[’

Thanks!

Not_a_Robot | 2016-07-12 22:32

Although using an object as root is often the best choice for future maintainability (you could easily change or detect what it contains), having an array as root is valid JSON. There is an open issue about it on Github.

Zylann | 2016-07-12 22:52

Great, thank you A LOT!

Not_a_Robot | 2016-07-13 04:31