Parsing JSON API using Shortcuts and Javascript

richard moult
4 min readSep 23, 2022

--

Parsing JSON using macOS standard Shortcut actions can be very difficult, especially if the JSON is complex or you need to look for or filter out certain properties. Armed with some very simple knowledge of javascript you can make this a super simple exercise.

Don’t let javascript scare you off, you are just about to find out how simple it can be to parse complex data structures.

In a previous post about parsing JSON, Monsieurdindon asked how it would be possible to extract certain information from a population data set. The answer is, yes it is possible, but the answer I came up with was quite complex and bespoke to that data set. Answering the question using standard actions would not really help to answer the question in a way that we can all learn from and apply easily to other situations.

So after a little research and trial an error this post will demonstrate the easiest way to handle more complex tasks when parsing JSON.

Quick recap — the problem

This url, https://datausa.io/api/data?drilldowns=Nation&measures=Population, provides the following population data structure.

{
“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”
}
….
]
}

If we wanted to extract the population for a certain year, our Shortcut would look something like…

Let’s see if we can make this complex 20 step Shortcuts a little simpler. Btw once we add in javascript we’ll need only 8 much simpler steps 😎.

Run Javascript Action

Shortcuts come with an action named Run Javascript on Active Safari Tab. This action is a little special to all the others and first requires Safari to be set up a certain way.

Set-up

Open the Safari app and select Preferences in the menu bar. Navigate to Advanced and select Show Develop menu in menu bar .

Next, back to the Safari menu bar you should now see a new option called Develop , select that and then Allow Javascript from Apple Events .

Ok, you are all set up now.

Javascript (JS)+ JSON

Next task is to learn a little about Javascript (JS) and JSON.

At this point we could dive right into JS, but for this post I’ll keep it as brief as possible. If you want to know more about JS please add a comment to this post and I’ll follow up with more info.

If the JSON data is expressed as an array, e.g [] , and we want to access the first item in that list then in JS it looks like data[0] , and the second items in the list would be data[1] etc etc.

If we want to run though every item in an array, it would look a little like

for (let i = 0; i < json.length; i++) {
// this line of code gets called for each item in the list
// so the first time i=0, then i=1, i=2 etc until i is end of list
}

If the JSON data is expressed as a dictionary, e.g {"ID Year": 2019} , if we want to access the value2019 we do so by using data["ID Year"] .

Putting that together with our population data structure above. It starts with {"data": [{ , which is a dictionary with “data”, where “data” is an array of dictionaries. So to loop through all the values to look for an “ID Year” equal to 2014 and find the population, it would look something like…

for (let i = 0; i < json["data"].length; i++) {
if (json.data[i]["ID Year"] === 2014) {
// population equals json.data[i]["Population"]
}
}

Action response

For the JS action to return a value in the Shortcut you need to end the JS with completion(x) where x is the value you want to return.

Final Shortcut

Quick run through, we need a shortcut to…

  • Ask the user for a year in order to return the population for that year.
  • Get the JSON content of the url.
  • Use JS to run through the JSON to look for the year provided in the first step and access the Population value.
  • If the population is found display the value, otherwise display an alert informing user that the data cannot be found.

Hope that helps to see how Javascript gives us the power to vastly reduce the complexity of actions required to extract bespoke JSON data in our Shortcuts.

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

You can find more ideas and tutorials here.

--

--