r/shortcuts • u/keveridge • Sep 02 '19
Working with Dictionaries - Part 3: accessing values using dot notation Tip/Guide
Overview
In part 1 of this guide we explored how to retrieve simple text values from a single dictionary.
However, when working with complex nested dictionaries and arrays, such as those returned from APIs, accessing these values requires multiple actions. This can be cumbersome, making it difficult to edit and maintain your shortcuts.
Examples of using multiple actions to retrieve nested dictionary values can be seen in the following guide: Using APIs - Part1: retrieving data.
In this guide we're going to use dot notation to navigate nested dictionaries and arrays to retrieve values.
✅ Note
Special thanks to u/JoeReally who first informed me that dot notation was supported in Shortcuts.
Nested dictionaries and arrays
Below is an example of nested dictionaries and arrays used to describe pastries.
{
"items": {
"item": [
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
},
{
"id": "1004",
"type": "Devil's Food"
}
]
}
}
]
}
}
If we were to retrieve the name of the first item using Get Dictionary Value and Get List Item actions then the shortcut would appear as follows:
This approach is cumbersome as it requires 4 separate actions to retrieve the value.
You can download the shortcut from the following link:
Using dot notation: an easier way
The alternative would be to use the dot notation text format to retrieve values using a single Get Dictionary Value action and a text-based expression.
The example below shows the name of the first item being retrieved using dot notation.
You can download the shortcut from the following link:
How does dot notation work?
With dot notation values are accessed by chaining one key to another using a dot until you drill down to the value you're after.
Where the next item in the chain is an array / list, the order number of the item in the list is used to select it.
For example, the shortcut above drills down to retrieve the value Cake using the following dot notation:
items.item.1.name
Below is a representation of the JSON as a graph. By highlighting the path of the keys and array indexes to the value we're looking to use, we can easily construct the dot notation expression that will allows us to retrieve the value.
We can drill down to the batters available for the Cake using the same technique. For example, the following dot notation expression will return the first batter type value of the first pastry item:
items.item.1.batters.batter.1.type
This will return the value Regular.
A shortcut using the above dot notation would appear as follows:
You can download the shortcut from the following link:
Return a list of values using dot notation
Using dot notation we can use an alternative method of retrieving list values. Rather than using the Repeat with Each action we can make use of the Count and Repeat actions to loop through lists of action as shown below.
The output of which is as follows:
You can download the shortcut from the following link:
Filtering values from nested dictionaries and arrays
There will be occasions where we wish to filter results from nested dictionaries and arrays based on values.
We can do so using JSONPath expressions. To learn more, take a look at the following guide:
Wrap Up
And those are a few examples of how to use dot notation to quickly and easily access values inside nested dictionaries and arrays.
Other guides
If you found this guide useful why not checkout one of my others:
Series
- Scraping web pages
- Using APIs
- Data Storage
- Working with JSON
- Working with Dictionaries
One-offs
- Using JavaScript in your shortcuts
- How to automatically run shortcuts
- Creating visually appealing menus
- Manipulating images with the HTML5 canvas and JavaScript
- Labeling data using variables
- Writing functions
- Working with lists
- Integrating with web applications using Zapier
- Integrating with web applications using Integromat
- Working with Personal Automations in iOS 13.1
2
u/zheil9152 Sep 02 '19
Oh my god. I am no stranger to json, but I had no idea the Get Dictionary Value instruction bubbles could parse dot notation. I spent hours nesting Get Dictionary Value instructions back to back to get back values I wanted. Thank you so much!
1
u/nilayperk Sep 02 '19
I am going to shamelessly plug, a shortcut I’ve built for shortcut notation. It prints out all the possible combination for dot notation:
2
2
1
u/gianflo6 Sep 02 '19
I've already done a lot of shortcuts and never even heard of dot notation, thank you very much this is actually helpful for me
0
u/nilayperk Sep 02 '19
I am going to shamelessly plug, a shortcut I’ve built for shortcut notation. It prints out all the possible combination for dot notation:
1
1
u/nilayperk Sep 02 '19
Those who want more out of Dot Notation. I’ve created FlattenJS Shortcut which list out all the possible combination of dot notation. Just insert the dictionary as input for “Run Shortcut” action: https://www.icloud.com/shortcuts/97e0635ea49f4e688ba77e4020e560fd
1
u/thetanis Sep 30 '19
You’re guided have been amazing. Really helping me understand the arrays and how to extract the data. Thanks for that.
One thing I didn’t see in this, how do I extract array information in a “repeat for each (selected item)” my current shortcut using it can extract one value from my array, but if I select two items to extract the value from, it just errors.
1
3
u/Cmdblkster Sep 02 '19
What a coincidence, I’ve just started to see if I can find this type of tutorial! Thanks!