Get the array's index of the user selection and extract information from array

Pau Sanchez Updated by Pau Sanchez

Only available for Landbot (not WhatsApp or Messenger) for WhatsApp please check this article

Dynamic Data block allow us to display an array of items coming from a variable that is an array. However we might need to get some extra information from that array, once we know the user selection.

One possible way would be to make another request to our API, but what about if we use the array that we already stored?

In this article we will show how to:

- Set Dynamic Data block to store the index (in the array) based on the user selection

- How to use Formula block to pull information from that specific part of the array

- Display the information

  1. Imagine that we pull from our Webhook (API) an array like the one below:
    {
    "records":[
    {"name":"Weber Compact 47cm",
    "description":"Let nature set the scene as crisp air bites your cheeks. Complete serenity, with only the crackle of charcoal breaking the silence.",
    "price":"109.90€",
    "pic_url":"https://www.thebarbequeshop.com/image/cache/catalog/Brands/Weber/WeberSmokeyJoePremium_1-600x600.jpg"},
    {"name":"Broil King Smoke Offset 500",
    "description":"The Offset Smoker 500 body is constructed from 2 mm / 14 gauge steel with 500 sq. in. primary cooking surface, heavy duty cast iron cooking grids.",
    "price":"899.00€",
    "pic_url":"https://www.thebarbequeshop.com/image/cache/catalog/Brands/Broil%20King/2020/offset%20500/955050-FRONT01-600x600.jpg"},
    {"name":"Weber Smokey Joe Premium 37cm Black",
    "description":"Let nature set the scene as crisp air bites your cheeks. Complete serenity, with only the crackle of charcoal breaking the silence.",
    "price":"109.90€",
    "pic_url":"https://www.thebarbequeshop.com/image/cache/catalog/Brands/Weber/WeberSmokeyJoePremium_1-600x600.jpg"}
    ]
    }

  1. In this example we only want to display the "name" key each product that inside the array "records". The array records is stored in the variable @records.

  1. Then, to display the "name" key we add it in the "Text for the button" input:

It will be like this:

  1. Now is when we set the Dynamic Data, to store the index by clicking the check box at the bottom:

  1. To store the index, properly we will create a variable, we will call it @index, but is important that is a "number" type
  2. Now the Dynamic Data block is set to get the index of the array, based on the user selection.

  1. Now we want to store the different keys (name, description, price and pic_url) in different Landbot variables (@selected_name, @selected_description @selected_price and @selected_pic_url). To do so, we will use the Formula block, with the GetValue() method:

  1. We will need a Formula, to store each variable. In this case we can see how, based on the array, we already have, we set the path to get to the index, and to the key name:

GetValue(@records, @index, "name")

Now in the variable @selected_name, will be stored the value of the key "name" based on the user selection.

  1. We will repeat the process for the other keys and variables we need:

  1. Now we are ready to display all the variables we collected

Below is the result

How did we do?

How to let user select a time of booking (with a minimum 45 minutes notice)

How to redirect visitors to a URL (web only)

Contact