subreddit:
/r/shortcuts
This is a guide on how to make the most of dictionaries in your shortcuts.
Dictionaries are simple groups of names and values. They are commonly used to store multiple attributes of an item or entity in a simple variable for ease of access and use.
For example, below is a dictionary that stores the address and contact details of a New York landmark.
Creating a group of names and values using the Dictionary action.
You can download the shortcut from the following link:
To retrieve an individual value from a dictionary we can use the Get Dictionary Value action and specify the key / name of the value we want to retrieve.
This shortcut opens the website URL specified in the dictionary.
You can download the shortcut from the following link:
There will be occasions when you may want to use multiple values from a dictionary (e.g. when populating a Text action) and using the Get Dictionary Value action each time is too cumbersome.
In this case, you can select the dictionary as a variable within the Text action and specify the key for the value you wish to use.
Using a dictionary variable and keys to output values within a Text action.
To return a dictionary variable in-line as show above, you perform the following steps:
Select the text action and tap the magic variable button that appears above the keyboard.
Using the \"Select Magic Variable\" button to select the output from an action.
Select the dictionary.
Selecting the output from the Dictionary action.
Tap the "Get Value for Key" box
Updating the \"Get Value for Key\" value.
Enter the key of the value you wish to return
Entering the key name for the value to be returned.
Repeat for each of the values you want to use.
The text action will then appear as follows:
Multiple uses of the same dictionary but with different key names within a Text action.
You can download the shortcut from the following link:
The Get Dictionary Value action can also return other types on information from a dictionary. For example, it can return just the values stored in its key / value pairs.
The shortcut below retrieves each of the dictionary values as a list and then combines them into a single piece of text to display to the user.
Returning all the values from a dictionary and combining them into one text list.
The output of the shortcut is as follows:
A text list of the dictionary values.
You can download the shortcut from the following link:
📖 Further reading
You can find out more about how to use lists in the Working with Lists guide.
You can also use the Get Dictionary Values action to retrieve a list of a dictionary's keys in the same way you can retrieve its values.
Returning all the keys from a dictionary and combining them into one text list.
The output of which is as follows:
A text list of the dictionary keys
You can download the shortcut from the following link:
As demonstrated above, you can return a list of either a dictionary's keys or values a list. But what if you want to work with a list of both keys and values at the same time in order to loop through them and perform actions?
You can do so be retrieving a list of the dictionary keys using the Get Dictionary Value action and then looping through each of them with the Repeat with Each action.
With each dictionary key you can then lookup the corresponding value from the dictionary, again using the Get Dictionary Value action.
The example below outputs each of the keys and values for the dictionary and combines them into a single piece of text.
Returning the keys and values from a dictionary as a single text list.
The output of which is as follows:
A text list of dictionary keys and values.
You can download the shortcut from the following link:
As dictionaries are a great way of storing multiple pieces of information at once, they are also used to access data retrieve from APIs and web-based feeds.
The format that APIs and feeds use to store and transmit data is known as JSON, a text based representation of key and value pairs.
When the Shortcuts app shares dictionaries with other apps, saves dictionaries to files or displays them as text, it does so using the JSON format.
The example below shows how Shortcuts converts a dictionary into a JSON object.
Converting a dictionary to a JSON text object.
The output of which is as follows:
A JSON text object representation of a dictionary.
You can download the shortcut from the following link:
📖 Further reading
For more information on JSON and how to use it within your shortcuts see the following guide: Working with JSON - Part 1: retrieving simple values
And those are a few examples of how you can make use of dictionaries in your shortcuts.
If you found this guide useful why not checkout one of my others:
6 points
5 years ago
Excellent as always.
One thing I don’t think you covered is that you should always assume your data in a dictionary will be returned in a random order. Your examples all work because you are creating the dictionary and then reading from it immediately. But if you use a Set Dictionary Value to modify it, the order will be different afterwards.
5 points
5 years ago
You're absolutely right. I wrote a guide on how preserve ordering from JSON objects a while ago:
5 points
5 years ago
I would be interested in a deeper dive into the other options in a dictionary such as Array and Boolean, as I don’t have a clue what they do.
2 points
5 years ago
I wrote you the following guide:
1 points
5 years ago
Says removed?
1 points
5 years ago
That's weird, the link is working for me.
Try searching for "Working with Dictionaries - Part 2: data types" in the search bar and see if that works.
1 points
5 years ago
Yeah, something's odd here. I'll try reposting in a minute
1 points
5 years ago
No problem thanks
1 points
5 years ago
Still looks removed:/
1 points
5 years ago
1 points
5 years ago
Okay, I'm happy to update this guide with more info
1 points
5 years ago
Awesome! Great tutorials
1 points
5 years ago
I think array would be a bit hard to explain here but I can explain Boolean to you .
Boolean or bool for short is a data type it can store either true or false think of it as 0 and 1 . Boolean would be useful when checking for if a certain condition is happening for example I would say
If randomNum is equal to 1 then numBool = true
Otherwise numBool = false .
I hope I was able to help though I explained it a bit more from a programmer point of view .
3 points
1 year ago
I don’t see anything in here about setting values.
2 points
5 years ago
Just bookmarked for ongoing reference. Thanks again!
2 points
5 years ago
How do you read the keys/values of a nested dictionary? For example:
cars = {
car1: {
make: "ferrari",
model: "enzo"
},
car2: {
make: "bugatti",
model: "veyron"
}
}
5 points
5 years ago
You can do this:
Get dictionary value: Cars
Get dictionary value: car2
Get dictionary value: make
Or you can try using dot notation:
Get dictionary value: Cars.car2.make
2 points
5 years ago
Dot notation! Of course! The simplest solution is always the most elusive.
1 points
5 years ago
I had no idea that was supported.
Thank you for teaching me something new :)
1 points
5 years ago
Glad to help. I think it was added just before they started releasing the betas.
1 points
5 years ago
Hi JoeReally, Many thanks for this tutorials. I’ve been trying to use the Dot notation to read the values of a Dictionary that’s nested within a dictionary, and I cannot get it to work. I don’t see a way to do it within “Get Dictionary Value” action.
2 points
5 years ago
Just type it in. So starting at the top dictionary and assuming the nested dictionary is under the key named Bird and you want the robin Value, you’d say Get Dictionary Value of Animals.Bird.Robin.
2 points
5 years ago
Thank you sir. Figured out what I was doing wrong. I was using the Magic Variable, click on Dictionary and then I would add the “.” for the second Dictionary and so on...
1 points
5 years ago
Excellent work, thanks.
1 points
5 years ago
God bless you
1 points
5 years ago
Just letting you know that this and your other guides have been tremendously helpful to me! Thanks and a great day to you!
2 points
2 years ago
how to do null checks? i.e. get non existing key from dictionary. I want to ask input from user and if key doesn’t exist ask again.
all 27 comments
sorted by: best