Specifying the format for a value

You can enforce the JSON data type used in the request output for each value. In addition, you can manipulate strings by:

  • indexing, which makes it possible to refer to individual characters in a string

  • slicing, which means extracting a substring

  • reversing, and

  • find/replace, which also supports using regular expressions.

To do this, add the specifier at the end of the data key in the JSON code.

Example

If you add the following JSON code:

{
    "request": {
       "body": {
          "contact-priority!s": "{contact.priority}", // convert number to string
          "contact-language!sl": "{contact.language}", // convert string to lowercase
          "contact-source-reversed!s[::-1]": "{contact.source}", // reverse string
          "contact-source-sans-first!s[1:]": "{contact.source}", // drop first character from string
          "contact-source-plus-to-zeroes!sr('+','00')": "{contact.source}", // replace '+' with '00' in string
          "contact-source-plus-removed-reversed-sliced!sr('+','')[7::-1]" // remove '+', reverse, and pick 8 last characters from the string 
          "contact-queue!js": "{contact.queue}" // convert object to JSON string
          "contact-queue-address-masked!sre('[^@]+(@[^@]+)','****\\g<1>')": "{contact.queue.address}", // mask username part of address
          "contact-queue-address-username!sre('([^@]+)@[^@]+','\\g<1>')": "{contact.queue.address}", // extract username part of adddress
          "contact-queue-address-domain!sre('[^@]+@([^@]+)','\\g<1>')": "{contact.queue.address}", // extract domain name part of address
          "contact-cad-from-cmi!j": "{contact.cad.from-cmi}" // convert JSON string to object
       }
    }
}

If you have the following contact object data at runtime:

  • Priority: 50
  • Language: "EN"
  • Source: "+358912345"
  • Queue:
    {"id": "C9177D585DA5EE11B3ED58961D1810CC", "address": "chat.queue@acme.com"}
  • CAD:
    {"from-cmi":
        "{\"key-1\":\"value-1\",\"key-2\":true}"}

The data will be resolved in the output as:

{
   "contact-priority": "50",
   "contact-language": "en",
   "contact-source-reversed": "543219853+",
   "contact-source-sans-first": "358912345",
   "contact-source-plus-to-zeroes": "00358912345",
   "contact-source-plus-removed-reversed-sliced": "43219853",
   "contact-queue": "{\"id\":\"C9177D585DA5EE11B3ED58961D1810CC\",\"address\":\"chat.queue@acme.com\"}",
   "contact-queue-address-masked": "****@sms.acme.com",
   "contact-queue-address-username": "chat.queue",
   "contact-queue-address-domain": "sms.acme.com",
   "contact-cad-from-cmi": {
     "key-1": "value-1",
     "key-2": true
  }
}
Table 1. Value format specifiers
Specifier Description Indexing and slicing If conversion fails
!i Converts resolved value to integer. Use value as-is
!f Converts resolved value to float. Use value as-is
!j Converts resolved value from JSON string to object. Use value as-is
!js Converts resolved value to JSON string. Use value as-is
!s Converts resolved value to string.
!sr

Converts resolved value to string and does a find/replace according to given instructions.

The find/replace operation is case-sensitive.

!sl Converts resolved value to string in lowercase.
!slr

Converts resolved value to string in lowercase and does a find/replace according to given instructions.

The find/replace operation is case-insensitive.

!su Converts resolved value to string in uppercase.
!sur

Converts resolved value to string in uppercase and does a find/replace according to given instructions.

The find/replace operation is case-insensitive.

!sre

Converts resolved value to string and does a find/replace using regular expressions according to the re.sub function's pattern and repl syntax.

The find/replace operation is case-insensitive.

!slre

Converts resolved value to string in lowercase and does a find/replace using regular expressions according to the re.sub function's pattern and repl syntax.

The find/replace operation is case-insensitive.

!sure

Converts resolved value to string in uppercase and does a find/replace using regular expressions according to the re.sub function's pattern and repl syntax.

The find/replace operation is case-insensitive.

String indexing

Indexing makes it possible to refer to individual characters in a string. The indexing numbers begin from 0. For example, in the string "Hello", the indexing is as follows:

Character H e l l o
Index 0 1 2 3 4

It's also possible to use negative indexing, where -1 refers to the last character. For example, in "Hello":

Character H e l l o
Index 0 1 2 3 4
Negative -5 -4 -3 -2 -1

To refer to a character, use square brackets with the index:

  • Positive index: string[0] returns "H"

  • Negative index: string[-1] returns "o"

String slicing and reversal

You can slice a string, which means extracting a substring, and to reverse the string. The syntax is:

string[start:end:step]

  • start: The index to start slicing (optional, inclusive). By default, slicing starts from the beginning of the string (value is 0).

  • end: The index to end slicing (optional, exclusive). By default, slicing ends at the end of the string (value is the length of the string).

  • step: The interval between indexes (optional). By default, the value is 1, which means that every character is included. A negative value reverses the string.

Examples

For example, with "Hello":

Example Result Description
string[1:4] "ell" Characters from index 1 to 3
string[1:] "ello" Characters from index 1 to the end
string[:3] "Hel" Characters from the start to index 2
string[::2] "Hlo" Every second character
string[::-1] "olleH" Reverses the string
string[::-2] "olH" Reverses the string and picks every second character