Need help building a loop in RapidPro (iterate over a list and stop at the end)

Hi everyone,

I’m working on a RapidPro flow (integrated with CHT) where I retrieve a list of children from a household using a webhook. My goal is to loop through this list (webhook.rows) and, for each child, send specific data (like child_name, child_id, etc.) via another webhook.

Here’s what I’ve already implemented:

  • I initialize an index at 0 (results.index).
  • I access child data using webhook.rows[results.index].
  • After processing one child, I increment the index with @results.index + 1 and loop back to continue.
  • I use @((webhook.rows)[results.index]).value to extract values from each row.

My problem is: I’m struggling to detect the end of the list properly. I can’t find a reliable way to stop the loop once all rows have been processed.

So, how can I write a condition in a split by expression that checks if results.index is beyond the size of webhook.rows? Is there a way to determine the list length or to safely detect that the current index is out of bounds?

Thanks in advance for any help, or if you have a working loop example to share!

Hi @Justinho,

In the split by expression component , you can have the expression @results.index check against @(count(webhook.rows)).

Here is the full flow I tried with this approach:

This was the result of the simulator:

Note: For testing this, I used webhook.site to create a temporary webhook with this content:

{
"rows": ["apple", "ball", "cat", "doll"]
}
3 Likes

Thank you Binod for this solution, which has helped me solve this problem in my flows.

3 Likes

To loop through webhook.rows in RapidPro and stop safely at the end, use @results.index < count(webhook.rows) as your loop condition in the Split by Expression node—this ensures the loop continues only while the index is within bounds, preventing out-of-range errors and allowing you to process each item reliably.