Unit introduction
Welcome to the third unit of the “Introduction to Iterators and Array Aggregators” course.
By now you have built your own array using Parse JSON, and iterated an array into bundles.
This unit will introduce a new tool - the array aggregator.
If you have not already done so, you should look at the Text Aggregators in action unit in the Make Basics.
Aggregating an array
In the previous course An introduction to Aggregators, aggregation was defined as combining multiple bundles into a single bundle.
The exercises used the text and numeric aggregators to create a new string and number respectively.
This unit will demonstrate how to aggregate multiple bundles into a single array.
Additionally it will show you how to create an array containing only specified information.
Let’s think of this in a real-life example: imagine you want to cook a soup and you have a lot of different ingredients in your kitchen (fruit, vegetables, meat, cheese, etc). From all of these, you would select only specific ingredients to put into the pot and cook your soup!
Use case
In the upcoming exercise you will learn how to use an array aggregator in your scenario to extract the information that you need.
Let’s expand on the scenario from the previous unit: You are planning a party and you want to know the number of people who are attending, and their dietary preferences, so you know how much food to buy and what to prepare.
Think of each invite response as one bundle. You have three party invitation responses, this therefore equals three bundles.
You can use the array aggregator to create one bundle. Think of this as your shopping list.
Additionally you will add a text aggregator, so you can see the array and text aggregators side-by-side.
Let’s make a start!
Aggregator activity
Work through each stage of this before you continue.
Start by creating a new scenario named Make Intermediate 1-3. Add a Parse JSON module. You’ll use the same JSON from the previous two units. Add an Iterator - this is the same setup as the first few stages of the previous unit. Right-click the Parse JSON module and click Run this module only.
Open the Iterator and map all guests[]. Remember, the purpose of the iterator is to take the single bundle and split it into multiple bundles. Click Run once, and Run anyway when prompted with the “A transformer should not be the last module in the route” prompt, to see the result. You’ll see that your three collections from a single bundle turn into three separate bundles.
As this exercise aims to show the text and array aggregators side by side, let’s add a Router to your Iterator. The Router is designed to send the bundles to different modules. The next step is to add an Array aggregator to the top route.
Open the Array aggregator. Under source module, select Iterator. This means the data being aggregated is the output by the iterator, rather than the data from Parse JSON. Under Aggregated fields, you will see the variables available from each module. The Iterator allows you to select the total number of bundles / bundle order position / name / dietary preference / preferred music. The Parse JSON module only has ‘all guests’ as an Aggregated field. Select name and dietary preference - this is the only information you need for the aim of this exercise. Click OK to save the module mapping.
Next, add a Text aggregator to the second route. Under Source module, select Iterator. Toggle Show advanced settings, and for Row Separator select New Row. Click in the text field, and map name - dietary preference from the Iterator items. Click OK.
Finally, create a filter between the Iterator and Router. As the aim of this exercise is to identify which event guests have dietary preferences, map the dietary preference in the condition field. Set the text operator as not equal to. Type none in your field. Click OK. This will pass bundles that contain dietary preferences other than “none”, for example vegetarian, vegan etc. Give this a label of IF NONE. Save your scenario. Let’s explore the outputs.
Run your scenario
Let’s look at the results for this scenario. Click Run once, and Run anyway when prompted with any alerts.
As covered in An introduction to Aggregators, this prompt appears because nothing is happening with the transformed data. Make expects the data from these two modules to be used elsewhere, for example inserted into a Google Sheet, or sent via email. However, as part of this exercise is about viewing data, click Run anyway.
Messages Tools ⚠️ A transformer should not be the last module in the route. Array aggregator ⚠️ A transformer should not be the last module in the route.
JSON
The Parse JSON module creates a single bundle. This bundle contains an array with three collections, each representing one invite response.
Iterator
The single bundle is then iterated into three separate bundles.
The iterated bundles are passed through a filter. As two bundles meet the filter criteria, two bundles are passed through. These two bundles are then sent to the Router, which directs them to the Array Aggregator and the Text Aggregator.
Note: When a bundle passes through the router and filter, these actions do not consume credits.
Array aggregator
The Array aggregator creates a new array using the selected properties (name and dietary preference). This results in an array containing two collections instead of three.
This means you have aggregated an array from bundles and extracted specific items (name/dietary preference) to form your new array.
Text aggregator
The Text aggregator creates a text string based on the mapped fields (name / dietary preference). Since the Iterator sent three bundles towards the Router, and two bundles passed through the filter, two lines of information are output by the Text aggregator module as a string.
This means you have created a text string from bundles and extracted specific items (name/dietary preference).
How are the Text aggregator and Array aggregator different?
Both of these methods have achieved the desired output from the earlier aim - to create a shopping list based on dietary requirements. But the output and potential usage is entirely different.
Text Aggregator Output
The Text aggregator produces a single text string. For example:
Bundle 1: (Collection)
text: Paul - vegan
Lukas - vegetarian
The created text string has one purpose - to bring together the required information from the multiple bundles, and combine them into a single message. This string could then be used, for example, to send a message by a web service or SMS.
Array Aggregator Output
The Array aggregator produces a structured array of collections. For example:
Bundle 1: (Collection)
Array: (Array)
1: (Collection)
name: Paul
dietary_preference: vegan
2: (Collection)
name: Lukas
dietary_preference: vegetarian
__IMTAGGLENGTH__: 2
The purpose of the output of the Array aggregator is to aggregate bundles or collections of data items into a single bundle.
For example, having several invites full of information and extracting only the information that is needed. In this example, it was name / dietary_requirement.
Array aggregators also have a target structure field that can generate structures as expected for other apps that allow you to map arrays. For example, some email apps contain a “to email” field, where the email address output from an array can be mapped to.