Loop Over

Updated 7 months ago by Karan

What is Loop Over

In Byteline you can easily perform a task multiple times with a configuration of a single field called loopOver. Loop over allows the users to run the same task for each element of a loop.

You will see the below checkbox on most of the Byteline tasks for specifying a loop over array. When checked, it lets you specify the JSON path of an array to loop over. More about it soon.

JSON Introduction

JSON stands for JavaScript Object Notation. Byteline uses JSON format for the flow data. It is easy to read, write, and understand for humans as well as machines.

Two JSON constructs are important to understand data in a Byteline flow. JSON Arrays and objects.

JSON Object

The JSON object is enclosed in curly braces { } and written in key-value pair. It can have nested data when necessary. For example, a person can be represented as below JSON object

{
"name": "Jack Smith",
"email": "jack@example.com",
"address": {
"city": "Philadelphia",
"state": "PA"
}
}

JSON Array

A JSON array is a sequence of values, which can be JSON objects. The values are separated with commas and are wrapped in square [ ] brackets. You can access array values by index. For example, multiple persons can be represented as below JSON array;

[
{
"name": "Jack Smith",
"email": "jack@example.com",
"address": {
"city": "Philadelphia",
"state": "PA"
}
},
{
"name": "Susan Jones",
"email": "susan@example.com",
"address": {
"city": "Fremont",
"state": "CA"
}
}
]

For more details on JSON, check out this w3school article.

Specifying Data in Looped Array

While configuring a task node of a flow, you will get an option to run the task for each element of a JSON array.

Follow the below-mentioned steps to specify data in a looped array:

Step -1: Create your flow with the required task nodes. To create your flow design, check out how to create your first flow.

For this document, we are assuming a flow with Simple HTTP and Airtable Upsert (update or insert) nodes.

Simple HTTP Loop Over

A Simple HTTP task is used to make an HTTP call to retrieve or send data. Loop over array can be specified to run this task once for each array element. To use data from the array element, you can use the @ notation as discussed below. Byteline also provides a Select Variables tool to help.

Follow the below-mentioned steps for loop over configuration:

Step -1: Click on the Edit button of the simple HTTP node to configure it .

Step -2: The node configuration window of simple HTTP will appear on the console. Select HTTP method and the URL to be called.

Step -3: Click on the Loop Over Checkbox. This will enable a text box where the loop over array can be specified.

Step -4: Click on the Select Variable Tool to pick an array from your flow. You can easily pick the JSON array to iterate using this tool.

Step -5: Click on the Grey button of the array to copy the array path.

Step -6: Paste the array path in the text field of the loop over to execute an action over the array repetitively in a loop. JSON path is used to specify the array. To read about JSON path, follow JSONPath Syntax section in this article.

Step -7: To use data from each index of the loop over array, you can again use the Select Variable tool. But this time, you will copy the child element of the array. Click on the Grey button of the element of the array that you want to copy.

Step -8: Paste the specific data element of the array to any of the task configurations. If you select JSON element under the loopOver array, then @ notation should be used. The pasted value is automatically converted to the @ notation.

In case the value is not automatically converted, you can easily do it manually. @ refers to the currently iterated array element. For choosing its child element, you need to use . notation. For example, if each array element is a person (as in the JSON array example above), you can refer to the city as @.address.city

If the selected JSON element is not a child of the loop over, then you can use regular ${} notation, but in this case, the values of that field will always be the same for each loop iteration.

Step -9: Click on the Save button of the configuration window.

The loop will execute for each value of the array element and will generate an updated value of the specified data element. The output of each loop will be sent to the third-party system.

Airtable Loop Over

In the Airtable task, loop over is used to upsert (update on insert) data from each array element to the Airtable table.

Follow the below-mentioned steps for loop over configuration. This is similar to what has already been discussed for using it in the Simple HTTP task. This section will provide you another example of using loop over for a different task.

Step -1: Click on the Edit button of Airtable node for loop-over configuration.

Step -2: Enter your Airtable base Id, table name, view, and click on Loop Over Checkbox in Airtable records configuration window.

A loop over text field will appear on the console.

Step -3: Click on the Select Variable Tool to pick an array from your flow.

Step -4: Click on the Grey button of the array to copy the array path.

Step -5: Enter the array path in the text field of the loop over to execute an action over the array repetitively in a loop.

Step -6: Click on the Grey button of the element of the array for which you want to update the records in Airtable.

Step -7: Paste the data element of the array with @ notation in the value section, to get the current value of the element.

Click on the Save button.

The data elements specified in the array will get updated each time the loop runs. Thus, the loop generates a new value each time and updates Airtable record.

Hence, you have successfully configured loop over in simple HTTP and Airtable node.


How did we do?