Guide: Dictionary Blocks

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

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

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

1.3 — Pairs

pair
It is the block of the dictionary pairs 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 exists, it returns the corresponding 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 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

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

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 keys must exist and be valid except for the last one.

1.6 — remove entry for key

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

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

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

1.8 — is key in dictionary?

is key in dictionary
Checks whether the key exists in the dictionary.

1.9 — size of dictionary

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

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

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 a dictionary into another dictionary. If the key exists, 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 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

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 the is a list? block, this tests if the given value is a dictionary or not.

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 go 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 stuck on the list by walking key path block, check out this detailed explanation by @yamafu

3 — Reference

41 Likes
[Free] Json To Dictionary Extension
How to make an extension PLZ SHOW CODING
Get all values in Tiny DB
Problem with get tags and values with for loops, Firebase
Create query ( filter and sort ) in firebase database
Help me create this json with dictionary
Survey Apps from MYSQL using JSON
How can arrange all the tag data dynamically in the cardview?
How to implement any API in my app
Help me to translate the date format by json
How to use Start value block?
Select ID when long click on item of a listview that is using a dictionary
How to only name
Require seperate section for name in variable
Help csv data to tinydb or list
Help with JSON : how to get a value?
How should I work with this JSON?
List and sublist continuing
Error when logging in by CPF
[Free] Extension JsonUtils
How to store text under categories
How to create different list as per different category
Create JSON file programmatically
How to use a JSON in your kodular application?
Decode Json with Dictionary block
XML parsing problem
Woocommerce integration
[Firebase + JSONTools] How to get a value from response?
Json data arranging with dictionary
Get image url from Json
How can i get all the data from web source in a list?
How to parse this json
How to parse this json
Please help with the practical usage of "Dictionaries" section
JSON File Parsing - In list veiw
How to get a text from Web Component
How To Get JSON data from Quran Cloud API
Number in JSON only getting 5 decimals
Kodular List to Json
Json data help me
Read specific sub object and Json array

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