0
votes

I'm passing JSON data to a Twilio Studio flow that has array data. Let's say that they're addresses. I would like to iterate over this array, and have multiple widgets inside each iteration to say the address, then capture the user saying 'yes' or 'no' to confirm that each address is correct.

Where I seem to be failing is using a flow variable as the index for the array. Below is the code inside of a Say/Play widget. flow.variables.address_idx is defined in a previous Set Variables widget as {{ 0 }}. flow.variables.idx is defined as 0. The comments are not part of the code, but are more succinct than the Liquid syntax.

//this says 'zero'
The current index is {{flow.variables.address_idx}}.

//nothing
The address is
{{flow.data.addresses[flow.variables.address_idx].service_address_street_1}}

//nothing
The address is 
{{flow.data.addresses[flow.variables.idx].service_address_street_1}}

//this works
The address is {{flow.data.addresses[0].service_address_street_1}}

//this works
{% assign foo = 0 %}
The address is {{flow.data.addresses[foo].service_address_street_1}}

It seems like the index isn't being treated as an integer when I use the variable in the flow.variables scope, but is instead being treated as a string. String keys for hashes seem to work okay.

Is there a syntax trick I'm missing? Or is there another approach for using multiple widgets in the body of an array iteration?

1

1 Answers

0
votes

Twilio developer evangelist here.

It's possible that the flow widget is serializing your integer index to a string. Have you tried casting the variable to an integer?

According to this answer, you can do it with a math filter:

{% assign foo = flow.variables.address_idx | plus: 0 %}
The address is {{flow.data.addresses[foo].service_address_street_1}}

Let me know if that helps at all.