Unit introduction
You are now at the second unit of the “Introduction to iterators and array aggregators” course within the Make Intermediate learning path.
In the previous unit you learned about how to parse JSON in a scenario.
In this unit you will build on your knowledge, learn how to work with iterators and why they are useful. You will also build a scenario where you will iterate a set of data from the JSON module.
Introducing iterators
You might remember briefly covering iterators in the Make Foundation level in the unit about the different types of modules. The time has come now to go into more details about iterators.
Iterators in Make
An iterator is a special type of module in Make that allows you to split an array into multiple smaller bundles. Iterators work only with arrays.
An iterator will extract an array (a list or a group of items) and split it up into a series of bundles. Each array item will output as a separate bundle.
For example, if you receive an email containing several attachments (an array), you can use an iterator to separate each attachment into individual items (separate bundles). This enables you to handle each attachment separately.
Specialized iterators
Many Make apps have specialized iterator modules with a simplified setup.
For example, the Gmail app contains the special iterator module called List email attachments and media. This module will iterate attachments that will produce the same results as the standard iterator, without you having to specify the array to iterate. In this case, it is enough to just specify the email ID from the source module.
You can of course use the normal Iterator module instead, but it’s good to know these exist too!
A real-life example
To give you a real life example of how an iterator works, imagine you place a food order to a restaurant with a group of friends.
The order is packed in a single bag. Think of this as one array which contains a list of all the ordered dishes.
Once the order arrives, you are then handing each person’s order to them - creating individual bundles.
This is what the iterator achieves: it splits a list (array) into separate bundles.
Why are iterators useful
An iterator converts each array of items into individual separate bundles. This way you can process each element in the array separately and exactly as you need.
Unless you use an iterator, Make only processes the first item in an array. The iterator lets you transform your data by taking arrays apart, which then enables you to access each item in the array separately.
An example flow might be JSON (Parse JSON) → Iterator → Google Sheets (Add a Row).
Getting ready
Now you know what an iterator is and what it does. Great progress! You have to go through a couple of steps more before you start building your scenario with the iterator.
Go through all the steps before you continue.
In this unit, you will continue your learning journey by building on your previous knowledge of the parsed JSON. You will use the Iterator to iterate a set of data and output this into a Google Sheet.
Before you start building the scenario, let’s set up the Google spreadsheet!
To give you some context, imagine you are planning a party and need to get some information from your guests about their food restrictions and music they like.
Navigate to your Google Drive, and create a new Google Sheet called Make Intermediate 1-2. Name the sheet Party planner.
In your Party planner sheet, set the following headings:
- Name (A)
- Dietary restrictions (B)
- Preferred music (C)
The spreadsheet is ready and that’s it for now! You’ll see why it was necessary to do this in the next step!
Building a scenario
Now you are ready and you can start building the scenario! Make sure to go through all the steps.
Imagine you are planning a party. You want to create a scenario that will summarize the information of your guests (their dietary restriction and music preference) in a spreadsheet.
Your scenario will use the JSON module set up in the Make Intermediate 1-1 scenario. You will then use the Iterator to iterate each guest’s set of data and output this into a Google Sheet. You will need the JSON module set up in the previous unit. If you have not configured it already, now would be a good time to go back and do it.
Setting up the JSON Module
Click Create a new scenario. Start by copying/cloning the Parse JSON module from your Make Intermediate 1-1 scenario.
An alternative way to do this is to add the JSON module to your scenario and copy the JSON, and paste it into the JSON string field. Then click OK to save your module mapping.
The JSON string should look like this:
{
"all_guests": [
{
"name": "Paul",
"dietary_preference": "vegan",
"preferred_music": "indie"
},
{
"name": "Ivana",
"dietary_preference": "none",
"preferred_music": "rap"
},
{
"name": "Lukas",
"dietary_preference": "vegetarian",
"preferred_music": "rock"
}
]
}
The JSON module is your source module that contains an array with 3 objects (3 guests), each with 3 properties (name, dietary and music preference of the guests) inside it.
If you click Run this module only, you will see 1 bundle in your output, which is a collection, containing an array. The array contains 3 collections (1 for each guest).
The Iterator is very useful in this case, because one array may contain many bundles, and you might not know in advance how many bundles there are going to be in your array. The Iterator splits the array and creates a separate bundle for each collection, so you can process them separately.
Note: that arrays can actually have 0 items. It’s good to check if this is the case before trying to get a value from your array, as it can be a common misconception.
Adding the Iterator Module
The next step is to add the Iterator module. The iterator will split the array from your source module into 3 separate bundles (parts) so you can work with them further.
Click Add another module and search for Flow Control → Iterator. You can also find it on your scenario editor toolbar, under Tools → Flow Control → Iterator (green icon).
Add the Iterator module to your scenario. It expects an Array as input.
Map all_guests from the JSON module. In this case, there is just 1 item to select. You can recognize an array by the square brackets and a dropdown arrow in front. You can also hover over the item to see its data type.
Save the module mapping by clicking OK.
Note: that you cannot use an iterator as a trigger. An iterator requires the source data from another module, plus the following module to pass the processed data to. It makes most sense to place iterators in the middle of scenarios. You can end a scenario with an iterator, but you won’t get any use out of it.
Let’s inspect the output of your Iterator module. If you click Run once, you will see that your one input bundle was split into 3 output bundles.
Configuring the Google Sheets Module
Next, you want the iterated data to be added as separate rows in a Google Sheet. Add the Google Sheets → Add a Row module to your scenario. Connect to your Google account.
Map the previously set up Make Intermediate 1-2 spreadsheet and the Party planner sheet.
Under Values, select the items from the Iterator module:
- Under Name (A) map
name - Under Dietary restrictions (B) map
dietary_preferences - Under Preferred Music (C) map
preferred_music
Click OK to save your mapping and then save your scenario.
Testing the Scenario
Time to test your scenario! Click Run once.
Open the Make Intermediate 1-2 spreadsheet you had previously set up to see what information you have there! You should notice that you have 3 separate rows added:
| Name | Dietary restrictions | Preferred music |
|---|---|---|
| Paul | vegan | indie |
| Ivana | none | rap |
| Lukas | vegetarian | rock |
Great job! You have successfully used an iterator in your scenario!
Element - text / text and image (Portrait) - LARGE
To understand the flow of your data better, now would be a great time to use the Explain flow button and see the journey your data takes!
1 bundle of data = food order / a set of invites iterated bundles of data = an individual order of food / the information of one of the guests
What about credits?
JSON
As you might remember from the previous units, the first module (JSON) will count as one credit. The first module in a scenario runs once and it always counts as one credit, even if it does not return any bundles.
Iterator
The Iterator module will consume only one credit in this example, no matter how many items there are in the array. You can see here that there were 3 items in the array (3 bundles in the output) but the Iterator still consumed only one credit.
Google Sheets
The Google Sheets module here and in general any module that comes after the Iterator will use as many credits as the number of items in the array. In this case, it consumes 3 credits.
Note: If you want to save on credits in a similar scenario, you could use the Aggregator module (e.g. Text Aggregator) after the Iterator module. The aggregator is opposite to an iterator, it puts everything back together and uses only one credit. In this case, the output of the Text Aggregator would be a very long text string that you could use in further modules.