How to convert a number to a string and replace delimiters

Let's analyze a few examples using such variables in the list:

{
  "name": "Jack",
  "last_request": {
    "total": 12000.5,
    "isPayed": true
  },
  "array": [
    "a",
    "b",
    "c",
    "d"
  ],
  "names": [
    "Jacob",
    "Emma",
    "William"
  ]

Example 1

Replace helper works with strings, and can replace one part of a string with some others. For example, we have a string and its value:

"name": "Jack"

With the help of our helper, we change the letter in the name variable in this way:

{{replace name "ck" "ke"}}

You can replace some parts of the string with others wherever variables are used, for example, in Message component. If you do this, then the already changed value will come to the bot:

You can test it in the sandbox.

Example 2

Consider another example. In the array, in last_request there is total variable, which has the number 12000.5 in its value.

"last_request": 
 {
   "total": 12000.5
 }

Fractional parts of numbers in variables are written with a dot, but we need to send a number in a comma-separated message. And since total is a number, we can't use replace with it, as in the previous case, because replace only works with strings.

So before we can replace a dot with a comma, we need to turn a number into a string. Let's do it with stringify, you need this helper to bring different types of data to strings.

{{replace (stringify last_request.total) "." ","}}

Note that stringify is inside the parentheses. You can nest some helpers in others instead of variables by wrapping them in parentheses.

Now you can use replace to rename characters:

{{replace (stringify last_request.total) "." ","}}

Replace the the dot with a comma, so first put a dot "." in quotes then a comma ","

The value with a comma will come to the bot:

In this way, you can change any other characters in the variable, be it numbers or letters.

You can test it in the sandbox.

Example 3

Stringify can also convert objects with nested variables to a string. If you apply stringify to an object or list, it will turn it into a JSON format. This can be useful if you need to send an object through Request component, or output variables to the chat for logging and error detection.

Let’s output all variables and values from this list:

"last_request": 
 {
  "total": 12000.5,
  "isPayed": true
 }

By this helper:

{{stringify last_request}}

You can test it in the sandbox.

This helper can be used in a similar way if you need to output an array:

{{stringify array}}

For example, let’s output this array:

"array": 
 [
    "a",
    "b",
    "c",
    "d"
 ]

Helper {{{{stringify array}} works by analogy with the helper {{JSON stringify array}}.

You can test it in the sandbox.

Example 4

Strinigify has an additional parameter that changes the behavior of the helper — delimiter. Let's look at some examples of its application.

Again, replace the dot with a comma in the array:

"last_request": 
 {
  "total": 12000.5
 }

Use delimiter, instead of a combination of stringify and replace, as we did before.

Such an entry:

{{stringify last_request.total delimiter=","}}

Will work exactly like the entry parsed in the second example of this instruction:

{{replace (stringify last_request.total) "." ","}}


Using delimiter, you can change the number separators in a variable to any character that you specify in quotation marks after delimiter=

You can test it in the sandbox.

Also, using the delimiter parameter, you can change the delimiters in the array:

{{stringify array delimiter="|"}}

Where | is the character to which we will change the commas that were in the array.

If we just output an array in a message, we would see all its elements separated by commas in the message. Thanks to stringify and delimiter="|", we sent its elements through a vertical line.

You can test it in the sandbox.

As in previous cases, the separators can be changed not only to the sign from the example, but also to any other characters, letters, numbers.

In the last example, we output all the names from names list. We'll add spaces to make the record look neater. If you add spaces to the helper, the spaces will appear in the message.

{{stringify names delimiter=" | "}}

You can test it in the sandbox.

To the beginning ↑