Parsing JSON API lists with Shortcuts

richard moult
3 min readJan 18, 2022

--

A quick guide to parsing a JSON API list using Apple Shortcuts.

In this tutorial we will extract JSON data from an API which contains a list of data with a structure of a dictionary and display parts of that data back to the user.

If you are not familiar with JSON you can read more about it here https://www.json.org/json-en.html.

The API we are going to use it https://datausa.io/api/data?drilldowns=Nation&measures=Population, which produces an output like …

{
"data": [{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2019,
"Year": "2019",
"Population": 328239523,
"Slug Nation": "united-states"
}, {
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2018,
"Year": "2018",
"Population": 327167439,
"Slug Nation": "united-states"
}
....
]

The info returned is a dictionary with a key of data . The data key has a list of items, where each item is a dictionary. For this example we want our Shortcut to display a list of population count for each year in the data list. So we’ll need to first access the dictionary data and then for each item access the key Year and Population .

So let’s get to it….

  • Open the Shortcut app and select the “+” icon to create a new Shortcut
  • Add action “Get contents of URL”, change url to https://datausa.io/api/data?drilldowns=Nation&measures=Population
  • Add action “Get dictionary from Input”. Once added the input will automatically change to “Contents of URL”.
  • Next we want to access the dictionary of data which belongs to the key called data . So add action “Get Dictionary Value”, and amend the key to “data”. This will now return a list of items.
  • As we now have a list, add the action “Repeat For Each”. The item name should automatically change to data .
  • Inside the repeat loop, add action “Text”. And now comes the fiddly bit. Inside the text item type the special word “Repeat Item”. As you type this you should be presented with a drop down list, you must select “x Repeat Item”. If you are on a mac you will now need to double tap “x Repeat Item”, this will present another menu and in “Get Value for Key” enter “Year”.
  • In the text field, again type “Repeat Item”. Double tap “x Repeat Item” but this time use a key of “Population”
  • Underneath “End Repeat” add the action “Combine Text”.
  • Finally add action “Show” that should auto complete with “Combined Text”

That should give you something like…

which presents output like…

If you enjoyed this blog post, you’ll love the book packed with plenty of real-world examples and AI integration — dive deep into Shortcuts.

To help parse more complex JSON structures or apply some basic logic check out this post.

You can find more ideas and tutorials here.

--

--