Guide: Dictionary Blocks

0 — Basics

0.1 — What Is Dictionary

  • Dictionary is object structured like JSON, which is a standard to store dictionaries as text. It is a way to store information in an organized, easy-to-access manner. It stores a collection of pairs that consists of a key and the corresponding value. The key is a reference to the value, kind of like a variable where the variable name acts as a reference.
  • Dictionary in Kodular is a built-in block.
  • Object uses { bracket at the start, } at the end.
  • A colon is in between the key and the value, while a comma separates the pairs (just like a list).
{
    "name": "John",
    "age": 28,
    "gender": "male"
}

0.2 Array

An array is equivalent to a list in AI2/Kodular. In JSON, it starts with [ and ends with ]
Example:

{
    "name":"John",
    "age":28,
    "gender":"male",
    "children": [
        "May",
        "Tom",
        "Peter"
    ]
}

In this example, [“May”, “Tom”, “Peter”] is an array (note: spaces outside of quotation do not matter). When you get children, a list of children names is returned.

1 — Explanation of Blocks

1.1 — Create empty dictionary

create empty dictionary
Creates an empty dictionary, without any pairs. (JSON equivalent: {}) More pairs can be added afterwards. If you click on the blue wheel, you can add more pairs and turn it into a make a dictionary block (explained in 1.2)

1.2 — Make a dictionary

make a dictionary
Self-explanatory. This makes up a dictionary with given pairs of keys and values. The value can be a dictionary, which makes it nested. You can add more by pressing the blue wheel.

To create the dictionary in the first example:
image

1.3 — Pairs

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

1.4 — Get value for key

1.4.1 — By key

get value by key
Get the value with the given key. If the key 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
As mentioned, a dictionary can be nested, which means a dictionary can be inside a dictionary. This also returns a value. but the difference is that it attempts to “walk” or “travel” down a list of keys. This block saves you time by eliminating the need to use multiple get value for key blocks.

Tip: if the value you access is a list, you can use index number to access an item in that list.
In the example below, the key path [“child”, 2] would return 2nd item in the list that corresponds to the key child.

When the key path 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 sets 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 overwrite the existing value. This block becomes necessary if you begin with an empty dictionary.

1.5.2 — By key path

set value for key path
Similar logic to “get value by key path”, but it sets a value. It uses a list for the key path. However, all keys must exist and valid except the last one (which means if you wanna create a nested path with a set block, you must create the pairs one by one).

1.6 — remove entry for key

remove entry for key
This removes the pair with the given key in the dictionary. If there the given key does not exist, nothing happens.

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 keys from first level (not recursive). This means the keys from a dictionary inside the dictionary are not returned.

1.7.2 — get values

get values
It returns a list containing the values in the dictionary. This also not recursive, so the inner dictionaries are returned as a whole (see below).

1.8 — is key in dictionary?

is key in dictionary
Checks if 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 is not recursive, like 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)) is converted to a dictionary {"name":"John,"age":28,"gender":"male"}.

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 a given dictionary. This means that all 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 (the into dictionary will be modified and from dictionary will not). If the key exists, the value in the into dictionary 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 1st item of the key path is "names". It access “names” which is a list. 2nd item of the key path is list index 1. We will get {"name": "Peter","age": 30}. Lastly, we have the walk all at level block. This will visit all values of the dictionary {"name": "Peter","age": 30}, which are (Peter 30) in list form.

Another example:


In this case, it access "names", which is a list. Then walk all at level here would access the whole list of people, namely
[{"name": "Peter", "age": 30}, {"name": "John", "age": 27}, {"name": "Luke", "age": 34}].
Finally, the key "name" get all values corresponding to "name" in the above list.


More details 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 values at that level whether they are from a dictionary or a list. It can only be used with list by walking key path. See examples in section 1.14.

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 checks if the given parameter is a dictionary or not.

2 — Notes + Tips

  1. If you have a JSON string, use JSON decode:
Current best method using extension

Because of Kodular’s lack of built-in decode block, please use this extension developed by @Mohamed_Tamer
[Free] Json To Dictionary Extension

Web component method, but it might go wrong

From the Web component, then use list of pairs to dictionary
blocks

  1. If you want to write in JSON text, I recommend these tools:
    JSON Hero
    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

53 Likes
[Free] Json To Dictionary Extension
How to make an extension PLZ SHOW CODING
Create query ( filter and sort ) in firebase database
Get all values in Tiny DB
Problem with get tags and values with for loops, Firebase
Help me create this json with dictionary
Json data arranging with dictionary
[Firebase + JSONTools] How to get a value from response?
Woocommerce integration
Create JSON file programmatically
How to create different list as per different category
List and sublist continuing
How should I work with this JSON?
XML parsing problem
Help with getting values from a dictionary
How to get value
Array structure
Want to load just 10 item in DynamicComponent Card
Firebase The value entered by the user is at the bottom of the project bucket
Is there a list of json dictionary?
Get all row responce content
Error 1103 - post text
[FREE] JSON extension
[F/OS] Recycler List View - Render larger data sets efficiently using `RecyclerView` for AppInventor & Distros
Parsing JSON api response
How can I make the message sending connection via Evolution API
How to make the second spinner depend on the selection of the first spinner using google spreadsheet
How to make the second spinner depend on the selection of the first spinner using google spreadsheet
Not found, receive data from screen1
Convert list to dictionary got error
Baselinker API, database help me?
Help with text division
Questions about delivery app using Firebase
Help with JSON : how to get a value?
[Free] Extension JsonUtils
How to only name
Require seperate section for name in variable
Help csv data to tinydb or list
Select ID when long click on item of a listview that is using a dictionary
How to use Start value block?
Help me to translate the date format by json
How to implement any API in my app
How can arrange all the tag data dynamically in the cardview?
Survey Apps from MYSQL using JSON
Decode Json with Dictionary block
Read specific sub object and Json array
Json data help me
Kodular List to Json
Number in JSON only getting 5 decimals
How To Get JSON data from Quran Cloud API
How to get a text from Web Component
How can i get all the data from web source in a list?
JSON File Parsing - In list veiw
Please help with the practical usage of "Dictionaries" section
How to parse this json
How to parse this json
Error when logging in by CPF
How to store text under categories
How to use a JSON in your kodular application?
Get image url from Json

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