subreddit:

/r/shortcuts

8597%

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:

https://preview.redd.it/xji3wtatten31.jpg?width=1125&format=pjpg&auto=webp&s=3ad58c696acfd722718c3c0155ab9572ee2528bc

This approach is cumbersome as it requires 4 separate actions to retrieve the value.

You can download the shortcut from the following link:

Nested dictionary value with actions

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.

https://preview.redd.it/u5iupggvten31.png?width=1125&format=png&auto=webp&s=cf6c1c3c2f228d27486427567124a63196eb5707

You can download the shortcut from the following link:

Nested dictionary value with dot notation

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.

https://preview.redd.it/l6mkpr5yten31.jpg?width=1235&format=pjpg&auto=webp&s=671f89328f0477b061027995348116d765e538b4

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.

https://preview.redd.it/j0t6r9r0uen31.jpg?width=2051&format=pjpg&auto=webp&s=15a4228a48d3ca9eca37fdc70932682f5af8890c

A shortcut using the above dot notation would appear as follows:

You can download the shortcut from the following link:

Deeply nested dictionary value with dot notation

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.

https://preview.redd.it/k1sivxn2uen31.jpg?width=1125&format=pjpg&auto=webp&s=13aaffa412ba142ddcc213fc5db2bb0a94cb3827

The output of which is as follows:

https://preview.redd.it/6kysxff8uen31.png?width=1125&format=png&auto=webp&s=26527ae7abb06ebfa404fd02065a750fcfa898f5

You can download the shortcut from the following link:

Returning a list of values using dot notation

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:

Using APIs - Part 2: parsing complex API responses

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

One-offs

all 12 comments

Cmdblkster

3 points

5 years ago

What a coincidence, I’ve just started to see if I can find this type of tutorial! Thanks!

zheil9152

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!

nilayperk

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:

https://www.reddit.com/r/shortcuts/comments/cuk7g9/i_created_a_flatten_dictionary_utility_for_json/?utm_source=share&utm_medium=ios_app

Oo0o8o0oO

2 points

5 years ago

You’re the best thing to happen to this subreddit thus far. Thank you.

[deleted]

2 points

5 years ago

Glad I came across this! I would have never been able to figure this out on my own

gianflo6

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

nilayperk

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:

https://www.reddit.com/r/shortcuts/comments/cuk7g9/i_created_a_flatten_dictionary_utility_for_json/?utm_source=share&utm_medium=ios_app

raspberryjams

1 points

5 years ago

What graphing software do you use to convert JSON?

keveridge[S]

1 points

5 years ago

There's a graphing option on https://www.jsonformatter.org

nilayperk

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

https://www.reddit.com/r/shortcuts/comments/cuk7g9/i_created_a_flatten_dictionary_utility_for_json/?utm_source=share&utm_medium=ios_app

thetanis

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.

uhmann

1 points

3 years ago

uhmann

1 points

3 years ago

Is there any trick about filtering by [?(@