0 — Basics
0.1 — What Is Dictionary
- Dictionary is actually object 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. It is called a
pair
. It uses a key 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. - Colon is between the key and the value. 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, value between [ and ] is a array. When you call Child, a list of child name will return.
1 — Explanation of Blocks
1.1 — Create empty dictionary
It create 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 obvious. It makes a dictionary with you given pairs of key and value. 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 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 exist, it returns the coresponding value. If it doesn’t, it will return the value of the parameter “or if not found”
Example:
- value exist
- value doesn’t exist
1.4.2 — By Key Path
It is an advanced version of get value by key
. The difference is: it rather uses a list than a specific key. As said before, a dictionary (object) can be in a dictionary (object). If there are so many dictionaries in the dictionary, this block helps you.
Also, if there are a list in your dictionary, this block is useful too.
When the list contain 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 given key’s value to the value in “to
” parameter. 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 empty dictionary.
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 key must be valid and existed except the last one. One more thing: the value of the last key must be a dictionary.
1.6 — remove entry for key
This will remove the pairs with the given key in the dictionary. If there isn’t any pairs with the given key, nothing will be deleted.
1.7 — get keys & values
1.7.1 — get keys
It return a list of all keys in the dictionary. Note that it only give you the first layer of keys. That’s mean, the dictionary inside the dictionary will not be counted in the list.
1.7.2 — get values
It returns a list containing the values in the dictionary.
1.8 — is key in dictionary?
Tests if the key exists in the dictionary and returns true if it does, otherwise it returns false.
1.9 — size of dictionary
Returns the number of pairs in the dictionary. Note that it only count the first layer.
1.10 — list of pairs to dictionary
Convert list of pairs to 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 list of pairs have many limitation, making it into a dictionary is better to operate 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 dictionary into another dictionary. If the key is exist, 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 get value by key path, the list can contain three major thing: 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 walk 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 is a list?
block, this tests if the given thing are dictionary or not. Return true if it is, false otherwise.
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 be 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 stucking at list by walking key path block, check out this detailed explanation by @yamafu