I'm loading my json file from assets succesfully. I checked this by using

print (dialogues_json.keys())
print (dialogues_json.values())

But my trouble is that I can't understand how to get my another keys and values. Take a look:

{
	"WhichScene" :
	{
		"Template" :
		{
			"000" : {"name": "", "expression": "default", "text": ""}, 
			"choice" : 
			[
				{"text" : "Yes.", "next_id" : -1},
				{"text" : "No.", "next_id" : -2}
			],
			"00" : {"name": "", "expression": "default", "text": ""},
			"repeat" : {"amount": -1}
		}
	}
}

My Dictionary object is like a tree with depth more than 1. I was watching both documentation and tutorial videos of "GDquest" but I couldn't find answers. How can I get any key and its value? I hope I shouldn't put this all down in String variable and use functions for finding names of keys and values and symbols ':', '[]', '{}'. :'( Thanks in advance.

How you access data within a Dictionary depends on how the Dictionary is structured. To the best of my knowledge, there is no way to get any key from a dictionary with a depth more than 1, or at least none that I know of.

This is because if is possible to have more than one key with the same name. For example, the following:

{
	"Dog One" :
	{
		"Age": 4,
		"Color": "Brown",
	}
	"Dog Two":
	{
		"Age": 8
		"Color": "Black",
	}
}

In this example, it would be very difficult to get the "Age" key without moving through the Dictionary structure, as the "Age" key is used multiple times, making it very hard to know which key you are wanting.


So long as you know the structure of the data within the Dictionary, it accessing dictionaries within dictionaries shouldn't be too hard. What are you trying to access?

For example, if you want to get the data within "000", then the following should work:

#Assuming dialog_json is the dictionary shown above and
# it has been successfully loaded/populated...
# Get the WhichScene dictionary
var json_data_whichscene = dialog_json["WhichScene"]
# Get the Template dictionary
var json_data_template = json_data_whichscene["Template"]
# Get the 000 dictionary
var json_data_000 = json_data_template["000"]
print (json_data_000)
# Depending on your needs, you can do all of the accessing in a single step:
var json_data_000_single = dialog_json["WhichScene"]["Template"]["000"]
print (json_data_000_single)

How you retrieve the data really depends on how the data is structured, and what data you have to work with. The example above breaks the example down into multiple steps, which hopefully helps show it you can access the data.

Working with dictionaries becomes a little more complicated when you have user generated data that can vary in what is stored and the names used, but even that just takes knowing what you are working with and working with the data you have available.

Hopefully this helps :smile:

@TwistedTwigleg said: For example, if you want to get the data within "000", then the following should work:

#Assuming dialog_json is the dictionary shown above and
# it has been successfully loaded/populated...
# Get the WhichScene dictionary
var json_data_whichscene = dialog_json["WhichScene"]
# Get the Template dictionary
var json_data_template = json_data_whichscene["Template"]
# Get the 000 dictionary
var json_data_000 = json_data_template["000"]
print (json_data_000)
# Depending on your needs, you can do all of the accessing in a single step:
var json_data_000_single = dialog_json["WhichScene"]["Template"]["000"]
print (json_data_000_single)

Hopefully this helps :smile:

Yeah, so much thanks! It seems this is pretty comfortable to do! :o And it will work even if I use [] instead of {} in json-file, will it? Like you can see "choice"-key.

And it will work even if I use [] instead of {} in json-file, will it? Like you can see "choice"-key.

{} means a dictionary, while [] means an array.

Below I have put a simple explanation of both, though please note that what I wrote is a very simple explanation. If you want a more in-depth look at the differences and how to use the data structures, I would suggest taking some programming books/courses, as they will almost give better explanations.

A dictionary stores data that can be retrieved using a key, making it a key-value pair. So long as you know/have the key, you can use that key to get the data stored in the dictionary. Generally in a dictionary, the key is a string, though this is not required and depending on the programming language many different keys/values can be stored in a single dictionary. GDScript allows for multiple different data types to be stored in a single Dictionary.

In contrast, an Array stores data in a sequential order, where you can retrieve data by knowing it's position in the sequence. So long as you know the index, the position of the element within the array, you can access the data. In many programming languages, including GDScript, you can only access data within a list either through it's index, or by using a function defined by the list. Depending on the programming language, Arrays can hold more than a single data type. GDScript allows for multiple different data types to be stored in a single Array.


For example, this is how you can access the data within the "choice" key:

var json_data_whichscene = dialog_json["WhichScene"]
var json_data_template = json_data_whichscene["Template"]
# Get the choice array
var json_data_choices = json_data_template["choice"]
# Get the first choice in the array
var json_data_choice_one = json_data_choices[0]
print (json_data_choice_one)
# Get the second choice
var json_data_choice_two = json_data_choices[1]
print (json_data_choice_two)

Then you can access the data within json_data_choice_one/json_data_choice_two. :smile:

3 years later