apply to .json from GDScript

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

How can I apply to several dictionaries in .json file from GDScript?
For example, I have .json file with questions and answers:

{
    "someQuestion \nsomeAnswer" : 1,
    "anotherQuestion \nanotherAnswer" : 0
}

No problem, I can apply to this dictionary by

questions_file.open("file.json", File.READ)				                questions.parse_json(questions_file.get_as_text())

But what if I want to apply to another dictionary in .json?

{
    {
        "question" : 1
    }
    {
        "anotherDictionary" : 1
    }
}

(I’m not sure it’s the right way to write a several dictionaries in .json)

So, the question is how to apply to dictionary with “anotherDictionary” : 1in GDScript?
Thank you

What do you mean by apply to?

avencherus | 2017-11-08 08:57

:bust_in_silhouette: Reply From: Zylann

Your second example is not valid JSON. Maybe you want this instead:

[
    {
        "question" : 1
    },
    {
        "anotherDictionary" : 1
    }
]

Which is a list of dictionaries.
Unfortunately, Godot 2.1.x doesn’t have a way to load JSON where the root item is not a dictionary. You can workaround this by wrapping the data:

{
	"questions": [
	    {
	        "question" : 1
	    },
	    {
	        "anotherDictionary" : 1
	    }
	]
}

You can parse this into a dictionary, and your list of questions will be stored under the “questions” key :wink:
So for example, “anotherDictionary” can be accessed with jsonDict["questions"][1]["anotherDictionary"]