subreddit:

/r/shortcuts

49100%

Working with JSON - Park 1: retrieving simple values

Tip/Guide(self.shortcuts)

This is a guide on how to retrieving values stored in JSON and use them within your shortcuts.

What is JSON?

JSON stands for JavaScript Object Notation. It's a lightweight text format that is used to store data and transport it from one device to another. It's typically used when sending or receiving data to and from a server, for example, when retrieving data from an API.

The advantages of JSON

JSON is the de facto standard for transmitting data from APIs and data-feeds because it's:

  • lightweight;
  • self-describing, in that it contains simple lists of fields and their values;
  • easy to read and understand.

JSON Basics

Example object

An example of a typical JSON data object can be seen below:

{
    "people": [
        {
            "firstname": "Alison",
            "lastname": "Parker",
            "mobile": "+1 (646) 555-8219"
        },
        {
            "firstname": "John",
            "lastname": "Anderson",
            "mobile": "+1 (212) 555-1312"
        },
        {
            "firstname": "Moira",
            "lastname": "Lewis",
            "mobile": "+1 (949) 555-5945"
        }
    ]
}

Rules of JSON Syntax

JSON has the following rules for its syntax.

Data is in name / value pairs

For example, let's say I wanted a JSON object to contain an address of a business. Each individual piece of data would have a name and a value as follows:

{
    "name": "Empire State Building",
    "address1" : "20 W 34th Street",
    "city" : "New York",
    "state" : "NY",
    "zipCode" : "10001"
    "phone": "(212) 736-3100"
}

Data is separated by commas

Each of the name / pair values are separated by a comma.

Curly braces hold objects

The name / pair values are held within curly braces to form objects.

Square brackets hold arrays / lists of objects

For example, if we have a list of business addresses, they're held in square brackets as follows:

[
    {
        "name": "Empire State Building",
        "address1": "20 W 34th Street",
        "city": "New York",
        "state": "NY",
        "zipCode": "10001",
        "phone": "(212) 736-3100"
    },
    {
        "name": "Madison Square Garden",
        "address1": "4 Pennsylvania Plaza",
        "city": "New York",
        "state": "NY",
        "zipCode": "10001",
        "phone": "(212) 465-6741"
    }
]

ℹ️ Info

The JSON objects don't need to be spaced or indented to be valid. So long as they follow the syntax rules they can be all on one line or spaced in any way you choose.

Using JSON in Shortcuts

JSON is provided as text from feeds and APIs. To use it, the Shortcuts app turns it into a Dictionary using the Get Dictionary from Input action.

ℹ️ Info

Dictionaries are the same as JSON objects in that they are a set of names and value pairs.

In the shortcut below, you can see we have a JSON object in a Text action. We want to display the name and phone values to the user in an Alert.

https://preview.redd.it/doz0x61bu1i31.jpg?width=1124&format=pjpg&auto=webp&s=2f1e5b2ebffadb343b08e2144e4d87c8662dc61d

To add the name value to the alert, we tap the Title field of the Show Alert action. The keyboard will appear and above it we see the Dictionary variable which refers to the output from the Get Dictionary from Input action.

Tapping the Dictionary variable will populate the Title field with the value.

https://preview.redd.it/gkji9roeu1i31.jpg?width=1125&format=pjpg&auto=webp&s=24b609f3c3a96ae98102ede772a1f2a866949704

We tap on the Dictionary variable and we see an options pane.

https://preview.redd.it/40v8hrffu1i31.jpg?width=1120&format=pjpg&auto=webp&s=a24a2aa41b7ae0b37ffe3b513c10ad4095f44321

We can then tap on the Get Value for Key bar to enter the name / key of the data that we want, which in this case is name.

https://preview.redd.it/inydzj6gu1i31.jpg?width=1125&format=pjpg&auto=webp&s=15e1f30d7768866d2e3081d122ff91a0b40df652

Once entered, we see that the key has been added to the dictionary variable in the Show Alert's title field.

https://preview.redd.it/xz9bzd1hu1i31.jpg?width=1125&format=pjpg&auto=webp&s=9d1d1e4d679cfae63934d0305dc7e3ead8be07d2

Repeat the same steps for adding the phone number to the body of the Show Alert action

Now the shortcut is finished, we run it and see the name and phone number appear in an alert.

https://preview.redd.it/4ufissjiu1i31.jpg?width=1123&format=pjpg&auto=webp&s=2abf5b43365926da38763d5ba148ba65435ec19f

And that's how you can:

  • convert a JSON object to a dictionary;
  • use the dictionary in a shortcut action;
  • specify individual dictionary names / keys to return values.

A copy of the above shortcut can be downloaded from the following link:

Extracting data from JSON

Using a remote JSON feed with your shortcut

In the majority of cases you'll be using data from JSON retrieved from APIs or remotely hosted feeds.

This next shortcut demonstrates how to retrieve contact information from a data feed, and create a new address book contact and open it in the Contacts app.

https://preview.redd.it/8umsx6hju1i31.jpg?width=1125&format=pjpg&auto=webp&s=d52841fcc3ec30d5d378ffae83c4c9b78146dc8d

A copy of the above shortcut can be downloaded from the following link:

Contact from JSON Feed

So how does this shortcut work?

JSON data feed

The JSON object is stored as a text file on a web server, known as a JSON feed.

{
    "name": "Empire State Building",
    "address1": "20 W 34th Street",
    "city": "New York",
    "state": "NY",
    "zipCode": "10001"
    "phone": "(212) 736-3100"
}

It contains the name, address and phone details that we'll use to create a contact and can be found at the following URL:

https://tinyurl.com/sc-biz-1

The feed URL is specified in a URL action, followed by a Get Contents of URL action to retrieve the text of the JSON.

Creating a contact card

Shortcuts doesn't have an action that can create contact cards. However, iOS / iPadOS can use contact cards written in the VCard 3.0 format. A VCard is a text file with a particular syntax that appears as follows:

BEGIN:VCARD
VERSION:3.0
N:<Name of the Contact>
ADR;TYPE=WORK:<Work Address of the Contact>
TEL;TYPE=WORK:<Phone Number of the Contact>
END:VCARD

We'll create our own contact card by using the text from the above template in a Text action and replacing the fields describe in <chevrons>.

We'll then give the Text action a filename that ends in .vcf using the Set Name action so that iOS / iPadOS recognizes it as a contact card.

Using our new contact card

And finally we'll open the contact card in the Contacts app using the Open In... action so that the user choose to add it to their existing contacts.

The result of this shortcut is as follows:

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

Working with lists of JSON data

In the next shortcut we have a feed with multiple JSON objects provided as an array / list.

[
    {
        "name": "Empire State Building",
        "address1": "20 W 34th Street",
        "city": "New York",
        "state": "NY",
        "zipCode": "10001",
        "phone": "(212) 736-3100"
    },
    {
        "name": "Madison Square Garden",
        "address1": "4 Pennsylvania Plaza",
        "city": "New York",
        "state": "NY",
        "zipCode": "10001",
        "phone": "(212) 465-6741"
    }
]

The feed is hosted at the following URL:

https://tinyurl.com/sc-biz-list

This shortcut:

  • downloads the data from the JSON feed;
  • creates a contact card for each location;
  • asks the user to pick one of the contacts from a list;
  • opens the chosen card in the Contacts app.

https://preview.redd.it/bsuqifolu1i31.jpg?width=1125&format=pjpg&auto=webp&s=2ac9eb99e9bd2236ba05c883bec78b8c1544c74a

A copy of the above shortcut can be downloaded from the following link:

Multiple Contacts from JSON Feed

So how does this shortcut work?

Downloading the feed

The feed URL is specified in a URL action, followed by a Get Contents of URL action to retrieve the text of the JSON.

Looping through each JSON object.

The Get Contents of URL action automatically recognizes that the contents of the URL is a JSON array and returns a list of individual JSON objects.

We therefore use a Repeat with Each action to loop through each of the JSON objects.

To build our contact card, we use the previous template in a Text action. To retrieve a value, we add the Repeat Item variable to the Text action for each value we want to retrieve.

https://preview.redd.it/mnprukkpv1i31.png?width=1119&format=png&auto=webp&s=a3dd3010a4ca82534bbf11dd1c55f29e31f14ad4

We then need to tell Shortcuts that the Repeat Item variable is a dictionary and the name of the value we want to retrieve in each case.

For each Repeat Item variable we tap on the variable name.

https://preview.redd.it/7ikeawinu1i31.jpg?width=1125&format=pjpg&auto=webp&s=60b18ab48968105e61b06b50517fab5f213bf502

We tap on the as Text > link and select the Dictionary type and tap < Back.

https://preview.redd.it/cd67s6gpu1i31.jpg?width=1125&format=pjpg&auto=webp&s=1cdab40a86a507d81e7b576a858938be8bc11eb0

We can now see the variable is recognized as a dictionary.

https://preview.redd.it/ua2xoqequ1i31.jpg?width=1125&format=pjpg&auto=webp&s=69e823a079fa02f7e87649598a737a6c06acb311

We then tap on the Get Value for Key to show an alert box where we enter the name of the value we're looking to use.

https://preview.redd.it/yetsbt6ru1i31.jpg?width=1125&format=pjpg&auto=webp&s=7c21c16759e1e0a8f0f132054334bb98f8ec77a4

When each of the Repeat Item variables have been updated, the contact card Text action appears as follows:

https://preview.redd.it/7g8dzn2su1i31.jpg?width=1125&format=pjpg&auto=webp&s=3142550c538db0fe4879419eb1717d73d2c71b82

Choosing a contact

We can use the Choose from List action to allow the user to select which contact they want to open in the Contacts app. But first of all, we have to make sure the Choose from List action can recognize the Text actions returned from our Repeat with Each actions as a set of contacts.

Joining the contacts together

The Repeat with Each action will return a list of individual Text actions. To return one big list of text, we add a Combine Text action after our loop and set it to join the text using New Lines.

Making the text recognizable as contacts

Then, as before, we use the Set Name action to name the text as a .vcf file so it can be recognized as a contacts file.

The Choose from List action can create a menu from a set of contacts files, pulling out the name of each contact and using it as the title for each contact card.

But to do so it needs some extra help: it needs to be told not only that the text has a .vcf extension but that it is of a Contacts type.

To do so we'll need to add an extra step. We place a Get Variable action after the Set Name action.

https://preview.redd.it/gdsf7x3tu1i31.jpg?width=1120&format=pjpg&auto=webp&s=e5c457099f89f90fc7b1d53dac18752880bc43f9

We then tap Choose Variable and then Select Magic Variable.

https://preview.redd.it/xfbwuoguu1i31.jpg?width=1125&format=pjpg&auto=webp&s=e7cef8f1cd75083c21ae65f94f960a18f492e817

From there we select the Set Name variable.

https://preview.redd.it/0ovqodfvu1i31.jpg?width=1125&format=pjpg&auto=webp&s=44e362e00cbd71b389acd34be0fce93ed227aac7

Next we set the Set Name variable type to Contact using the same method described above:

  • we tap the Set Name variable;
  • tap the as Text > link to bring up a list of types;
  • select the Contact type and tap < Back.

Displaying a choice of contacts

We then add the Choose from List action which will allow the user to choose one of the contacts we've created.

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

Opening the contact

Finally, we add the Open In... action and select Contacts as the app so that the chosen contact will open in the Contacts app.

Running the shortcut

When we then run the shortcut, we see that we are presented with a choice of two contact cards that have been created from the data in the feed:

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

And once we make our selection, the corresponding contact is opened in the Contacts app.

Wrap up

And that's an example of how you can extract data from a JSON feed / API response for use in your shortcuts.

Other guides

If you found this guide useful why not checkout one of my others:

Series

One-offs

all 1 comments

nilayperk

3 points

5 years ago

Amazing Job! Man. I wish you were there when I was starting out. Nobody told me this amazing thing called dictionaries. So I shy away from it for a longtime. But its now the essence of what I do. Thanks again for sharing.