Guide: Dictionary Blocks

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

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

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:
image

1.3 — Pairs

pair
It is the block of the dictionary pairs for for constructing dictionaries.

1.4 — Get value for key

1.4.1 — By key

get value 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:

1.4.2 — By Key Path

get value 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

set value for 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

set value for 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

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

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

get values
It returns a list containing the values in the dictionary.

1.8 — is key in dictionary?

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

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

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

dictionary to list of pairs
This block does the opposite of list of pairs to dictionary.

1.12 — copy dictionary

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

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

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

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?

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

  1. 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.
blocks

  1. If you want to write JSON text, I recommend these tools:
    Json Parser Online
    Visual Studio Code
  2. If you are stucking at list by walking key path block, check out this detailed explanation by @yamafu

3 — Reference

35 Likes

Wow very nice guide! :fire::fire:

From many days, I was trying to understand dictionary but not understood.

But you did a great job. Everything is explained in detail.

Really great job! :sunglasses:

I appreciate your hard work. :fire::fire:

Thanks for the guide! :heartbeat:

3 Likes

Great work…
i like the way u explained the whole topic
Really helpful and a detailed guide :blush: :blush:

2 Likes

Thank you very much :smiling_face_with_three_hearts:

2 Likes

Great work ! :clap:

1 Like

Very detailed guide :heart_eyes:
It is hard to make guide . Great job @WatermelonIce

2 Likes

I made it wiki so that everyone can correct me if I have said anything wrong :wink:

1 Like

I have 2 questions:

  1. By reading your guide, I understand that we can store string and number as value in dictionary, is there anything else we can store in dictionary?

  2. Does the dictionary stores values as like tiny db so that we can call it later or as like global variable ?

Answer to your both questions is Yes.

1 Like

Almost everything can be stored

1 Like

I also have a guide about this:

1 Like

Nice a detailed Guide :ok_hand: Good.

2 Likes

Great guide @WatermelonIce i think you are expert in json

2 Likes

I don’t think this is a thing :sweat_smile:

1 Like

Hello
I appreciate this complete and easy-to-understand commentary. I also wrote a specific explanation of how to use the Dictionary feature of MIT App Inventor. Especially for “list by walking key path” and “walk all at level”. I would be happy if the following blog posts were any useful:

3 Likes

Wow, thank you for the detailed explanation! I am sure people will learn from it :blush:

OMG so good very helpful especailly for me :sweat_smile:

Thank you so much

2 Likes

Are these like objects in Javascript?

Yes, I think.


JSON = JavaScript Object Notation

2 Likes

Great guide. Super helpful

2 Likes