Table of Contents

Formulas - Object

Judit Updated by Judit

If this is your first time using Formulas, please check our Getting Started guide here and our crash course here.

Read this article for some practical examples of the possibilities to use "GetValue" and "ToJSON" with formulas.

Formulas available

  • GetValue: This method can be used to get the value of the attribute object. It doesnโ€™t take any parameters. It just returns the value of the Attribute object.
  • ToJSON: This method returns a date object as a string, formatted as a JSON date.

We will get into more detail about each one in this article:

GetValue

Get value from the key "Name"

Imagine that you have JSON like the one below, stored in a variable type array called "@response" and you want to capture the value located in key "Name" which is "Landbot".

{
"Name":"Landbot",
"Web":"landbot.io"
}

Here is how you can capture the value "Landbot":

GetValue(@response,"Name")
Get value from the first item of the array that is in the key "colors"

Imagine that you want an item that is inside an array. Let's say that you have the JSON below stored in a variable type array called "@response" and you want to capture "red", which is the first item of the array that is in the key "colors".

{
"materials":["iron"],
"colors":["red", "blue"]
}

Here is how you can do to capture the value "red":

GetValue(@response,"colors",0)

Get Value from a nested object

For nested objects, we will use nested formulas. So, let's say we have a JSON in a variable called @response with these values, and we want to capture the value "landbot.io" that is in the key "web", which is inside the key "details".

{
"name":"Landbot",
"details":{
"web":"landbot.io"
}
}

Here is how we will capture the value "landbot.io":

GetValue(GetValue(@response,"details"),"web")

ToJSON

Transform string to a JSON

There might be a case that you need to build a JSON out of values collected in Landbot. Using ToJSON, you can build it directly as below:

ToJSON('{"city":"Valencia","country":"Spain"}')

Create a JSON Object using user inputs (value as variable)

In this case we want to create an object using the variables "@name" and "@email":

ToJSON('{"name":"@name","email":"@email"}')

Create a JSON Object using user inputs (key and value are variables)

Let's say we want to let the user select 2 inputs, a category (we will use "country"), that we will store under the variable "@category", and the country selected which we will store under the variable "@country".

Then the formula will be like this:

As you see below, key and value are variables:

ToJSON('{"@{category}":"@{country}"}')

How did we do?

Formulas - Math

Get started with the Formulas block

Contact