0 — Basics
0.1 — What Is Dictionary
- Dictionary is an object structured like JSON. Therefore, you have to know the basics of JSON. It is a way to store information in an organized, easy-to-access manner. It is formed with a key and a value and it is called a
pair
. The key is used to call the value. If you still don’t understand, search on Google for more information. - Dictionary in Kodular is a built-in block.
- Object uses
{
bracket at the start,}
at the end. - A colon is between the key and the value. A comma is used to separate. Example:
{
"name":"John",
"age":28,
"gender":"male"
}
0.2 Array
Array is the same thing as list in AI2/Kodular. In JSON, it starts with [
and end with ]
Example:
{
"name":"John",
"age":28,
"gender":"male",
"Child": [
"May",
"Tom",
"Peter"
]
}
In this example, the value between [ and ] is an array. When you call Child
, a list of child names will return.
1 — Explanation of Blocks
1.1 — Create empty dictionary
It creates an empty dictionary, without any pairs. More pairs can be added afterwards. (Will explain later) If you press the blue mutator button, you can add more pairs and make it a make a dictionary
block (explained in 1.2)
1.2 — Make a dictionary
It is self-explanatory. This makes up a dictionary with given pairs of keys and values. The value can be a dictionary too. You can add more by pressing the blue mutator button.
Using the first example:
1.3 — Pairs
It is the block of the dictionary pairs for constructing dictionaries.
1.4 — Get value for key
1.4.1 — By key
This can call the value with the given key. If the value exists, it returns the corresponding value. If it doesn’t, it will return the value of the parameter or if not found
.
Example:
- value if exist
- value if doesn’t exist
1.4.2 — By Key Path
It is an advanced version of get value by key
. The difference is that it rather uses a list than specific keys. As I said before, a dictionary (object) can be in a dictionary (object). If there are multiple dictionaries in the dictionary, this block helps you.
Also, if there is a list in your dictionary, this block is useful too. By using the index number in the key path, you can access the list items directly.
When the list contains one item only, it is the same as get value by key
!
1.5 — Set value for key
1.5.1 — By key
This block can set the value of one pair with the given key. If the pair doesn’t exist in the dictionary, it will create a new one. Otherwise, it will replace the existing value. This is useful when you first create the empty dictionary I mentioned above.
1.5.2 — By key path
If you understand what I am talking about in the “get value by key path”, this is the same logic, but it set value rather than get value. It uses a list for the key path. However, all keys must exist and be valid except for the last one.
1.6 — remove entry for key
This will remove the pairs with the given key in the dictionary. If there aren’t any pairs with the given key, nothing will be deleted.
1.7 — get keys & values
1.7.1 — get keys
It returns a list of all the keys in the dictionary. Note that it only gives you the first layer of keys. That means a dictionary inside the dictionary will not be returned to the list.
1.7.2 — get values
It returns a list containing the values in the dictionary.
1.8 — is key in dictionary?
Checks whether the key exists in the dictionary.
1.9 — size of dictionary
Returns the number of pairs in the dictionary. Note that it only returns the first layer, the same logic as get keys
.
1.10 — list of pairs to dictionary
Convert a list of pairs to a dictionary. For example, the list ((name John) (age 28) (gender male))
, it will make it into a dictionary {"name":"John,"age":28,"gender":"male"}
. Because a list of pairs has many limitations, making it into a dictionary is better for operating different tasks.
1.11 — dictionary to list of pairs
This block does the opposite of list of pairs to dictionary
.
1.12 — copy dictionary
It makes a deep copy of the given dictionary. This means that all of the values are copied recursively, and changing the value in the copy won’t modify the original one.
1.13 — merge into dictionary
This will merge from a dictionary into another dictionary. If the key exists, the value will be overwritten.
1.14 — list by walking key path
This is similar to get value by key path
. It also uses a list for the key path, but it will return a list of value(s). However, unlike the get value by key path
, the list can contain three major things: dictionary key, list indices and walk all at level
(explained in 1.15).
Consider this dictionary:
{
"names": [
{
"name": "Peter",
"age": 30
},
{
"name": "John",
"age": 27
},
{
"name": "Luke",
"age": 34
}
]
}
The first item of the list is “names”. It walks through names and it is a list. Then, the next one is 1, which is a list index. We will get
{"name": "Peter","age": 30}
. Lastly, we have the walk all at level
. This will visit all values of the object (dictionary) at this point. Values of that point are (Peter 30) in list form.
Another example:
We will use the above dictionary as well.
In this case, it walks through names again, which is a list. But then, we put
walk all at level
here. We will get the whole list this time. Lastly, the key “name
” get all name in the list.
From documentation:
If the
walk all at level
is specified, every value at that point is followed in succession (breadth-first), at which point the walk continues from the next element in the path. Any element that matches the whole path is added to the output list.
1.15 — walk all at level
After reading the above block, we can conclude that it will visit all levels. In dictionaries, all values will be visited. It can only be used with list by walking key path
. See the list by walking key path block for examples.
From documentation:
This can be used to aggregate information from a list of items in a dictionary, such as the first name of every person in a database represented by JSON objects.
1.16 — is a dictionary?
Similar to the is a list?
block, this tests if the given value is a dictionary or not.
2 — Notes + Tips
- If you have a JSON string, use JSON decode:
Best method using extension
Because of Kodular missing blocks, please use this extension provided by @Mohamed_Tamer
[Free] Json To Dictionary Extension
Default method, but it might go wrong
From the web component, Then use list of pairs to dictionary
. This is useful when there are lots of data.
- If you want to write JSON text, I recommend these tools:
Json Parser Online
Visual Studio Code
- If you are stuck on the
list by walking key path
block, check out this detailed explanation by @yamafu