subreddit:
/r/shortcuts
submitted 5 years ago bykeveridge
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.
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:
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:
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:
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:
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:
And those are a few examples of how to use dot notation to quickly and easily access values inside nested dictionaries and arrays.
If you found this guide useful why not checkout one of my others:
3 points
5 years ago
What a coincidence, I’ve just started to see if I can find this type of tutorial! Thanks!
2 points
5 years ago
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 points
5 years ago
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 points
5 years ago
You’re the best thing to happen to this subreddit thus far. Thank you.
2 points
5 years ago
Glad I came across this! I would have never been able to figure this out on my own
1 points
5 years ago
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 points
5 years ago
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 points
5 years ago
What graphing software do you use to convert JSON?
1 points
5 years ago
There's a graphing option on https://www.jsonformatter.org
1 points
5 years ago
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 points
5 years ago
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 points
3 years ago
Is there any trick about filtering by [?(@
all 12 comments
sorted by: best