You can use the Flows Query Language (FQL) to perform math functions, manipulate strings and arrays, and interact with the data in your responses in several ways. Sample data and FQL examples are below.
The examples below use this JSON data:
{
"customer_info": {
"customer field": "Customer data",
"unformatted_customer_field": " customer \n stuff ",
"total_value": "281.01",
"associated_usernames": ["user1", "myuser", "online_user"]
},
"payments": [
{
"invoice_number": "101301",
"date": "2022-09-11T16:12:34.494Z",
"description": "recurring subscription",
"amount": 110.48
},
{
"invoice_number": "101302",
"date": "2022-09-29T14:45:13.148Z",
"description": "one time purchase",
"amount": 24.49
},
{
"invoice_number": "101303",
"date": "2022-10-11T16:12:34.683Z",
"description": "recurring subscription",
"amount": 110.48
},
{
"invoice_number": "101304",
"date": "2022-10-12T11:45:22.182Z",
"description": "recurring subscription deluxe",
"amount": 35.56
}
]
}
The example below gets every description
value in the payments
array and appends the string annual cost
. It then gets the amount
value below each description
field, multiplies it by 12, and appends it to its corresponding result. The results are grouped by description
field.
FQL | payments.{description & " annual cost" : amount*12} |
Result |
[ {"recurring subscription annual cost": 1325.76}, {"one time purchase annual cost": 293.88}, {"recurring subscription annual cost": 1325.76}, {"recurring subscription deluxe annual cost": 426.72} ] |
The $length()
function returns the length of the specified string. The example below returns the length of the string in the first description
key-value pair in the payments
array.
FQL | $length(payments[0].description) |
Result | 22 |
The $substring()
function returns part of a specified string. In the example below, The 3
is optional and specifies the offset, and the 6
is the number of characters you are selecting. Negative numbers can also be used for the offset.
FQL | $substring(payments[0].description, 3, 6) |
Result | "urring" |
In the example below, the $substringBefore()
function returns the substring before the specified occurrence of subscription
. If it doesn't find subscription
, it returns the entire string.
FQL | $substringBefore(payments[0].description, "subscription") |
Result | "recurring " |
The $substringAfter()
function finds a pattern and returns the substring that appears after the found pattern. The example below returns the substring that follows recurring
in the description
key-value pair in the first object of the payments
array.
FQL | $substringAfter(payments[0].description, "recurring") |
Result | " subscription" |
The $uppercase()
function makes all the characters in a string uppercase.
FQL | $uppercase(payments[0].description) |
Result | "RECURRING SUBSCRIPTION" |
The $lowercase()
function makes all the characters in a string lowercase.
FQL | $lowercase(customer_info."customer field") |
Result | "customer data" |
The $trim()
function does the following:
FQL | $trim(customer_info.unformatted_customer_field) |
Result | "customer stuff" |
The $pad()
function adds spaces or characters to a string so that the total length of the string equals the second parameter. If the second parameter is a positive number, it pads the end of the string with the third parameter. If the second parameter is negative, it pads the front of the string with the third parameter. (Third parameter characters default to spaces if left blank.)
FQL | $pad(customer_info."customer field", 15, "#") |
Result | "Customer data##" |
The $split()
function returns the string split on the separator specified in the second parameter and optionally limited by the third parameter. You can also use a regex instead of a string.
FQL | $split(payments[1].description, " ", 2) |
Result | ["one","time"] |
The $join()
function creates a single string from an array of strings. The example below gets the array from the associated_usernames
key and returns the array's values as a single string.
FQL | $join(customer_info.associated_usernames) |
Result | "user1myuseronline_user" |
In the example below, the $replace()
function finds the instances of recurring
in the first parameter string and replaces them with renewing
, limited to the first instance found (optionally specified with the 1
). You could also use a regex instead of recurring
.
FQL | $replace(payments[0].description,"recurring", "renewing", 1) |
Result | "renewing subscription" |
The $base64encode()
function converts a string to base64 encoding. The example below converts the string "some data here"
into "c29tZSBkYXRhIGhlcmU="
.
FQL | $base64encode("some data here") |
Result | "c29tZSBkYXRhIGhlcmU=" |
The $base64decode
function converts a base64-encoded string into a human-readable string. The example below converts "c29tZSBkYXRhIGhlcmU="
into "some data here"
.
FQL | $base64decode("c29tZSBkYXRhIGhlcmU=") |
Result | "some data here" |
The $jsonParse()
function enables the string to be formatted into JSON so it can be queried with FQL. The example below assumes you have the string {"Feedback Type":"Bug Report"}
stored as a variable named input
.
FQL | $jsonParse(input) |
Result | {"Feedback Type":"Bug Report"} |
You can convert a string into a number with the $number()
function. The example below converts the string "281.01"
in the customer_info
object into the number 281.01
.
FQL | $number(customer_info.total_value) |
Result | 281.01 |
The $sum()
function gets values from every instance of a key-value pair in an object or array, adds the values together, and returns the result. The example below gets every amount
field's value in the payments
array and returns their sum.
FQL | $sum(payments.amount) |
Result | "$281.01" |
The $abs()
function returns the absolute difference between two numbers. Absolute difference is the distance between two values on a number line. Absolute difference is always positive.
FQL | $abs(4.56 - 6.78) |
Result | 2.22 |
The $ceil()
function rounds a number up to the next whole number.
FQL | $ceil(3.45) |
Result | 4 |
The floor()
function rounds a number down to the previous whole number.
FQL | $floor(3.99) |
Result | 3 |
The $power()
function raises the first number to the power of the second number. The example below raises 2 (the base) to the power of 3 (the exponent).
FQL | $power(2,3) |
Result | 8 |
The $sqrt()
function returns the square root of a number.
FQL | $sqrt(9) |
Result | 3 |
The formatBase()
function converts a number to hex or binary. The example below converts 3000
to hex. Using base 2 instead of 16 would convert 3000
to binary.
FQL | $formatBase(3000, 16) |
Result | "bb8" |
The $round($random())
function generates a random whole number. The example below generates a random whole number between 1 and 1000 and returns the number appended to the string "Invoice number ".
FQL | "Invoice number " & $round($random()*1000) |
Result | "Invoice number 891" |
You can convert a number into a string with the $string()
function. The example below gets the number value from the amount
key in the first object in the payments
array and converts it into the string "110.48"
.
FQL | $string(payments[0].amount) |
Result | "110.48" |
The $encodeUrlComponent()
function replaces certain characters in a URL component with their UTF-8 encoded versions. The example below replaces ?
with %3F
, and =
with %3D
.
FQL | $encodeUrlComponent("?city=melbourne") |
Result | "%3Fcity%3Dmelbourne" |
The $decodeUrlComponent()
function replaces UTF-8 encoded characters in a URL component with their original versions. The example below replaces %3F
with ?
, and $3D
with =
.
FQL | $decodeUrlComponent("%3Fcity%3Dmelbourne") |
Result | "?city=melbourne" |
The $encodeUrl()
function replaces certain characters in a URL with UTF-8 encoded characters. The example below replaces こんにちは
with %E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF
FQL | $encodeUrl("https://faketranslatewebsite.com/?phrase=こんにちは") |
Result | "https://faketranslatewebsite.com/?phrase=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF" |
The $decodeUrl()
function replaces UTF-8 encoded characters in a URL with their original versions. The example below replaces %E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF
with こんにちは
.
FQL | $decodeUrl("https://faketranslatewebsite.com/?phrase=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF") |
Result | "https://faketranslatewebsite.com/?phrase=こんにちは" |
The $now()
function returns the current time in ISO 8601 format.
FQL | $now() |
Result | "2022-11-04T22:36:57.094Z" |
The millis()
function returns the current time in Unix milliseconds since the epoch.
FQL | $millis() |
Result | 1667601477254 |
The $toMillis()
function converts a given date format into Unix epoch time. See the formatting section below for details on date formatting.
FQL | $toMillis("10/12/2018 11:39 PM", "[M]/[D]/[Y] [h]:[m] [P]") |
Result | 1539387540000 |
The $fromMillis()
function converts Unix epoch time into a different date format. See the formatting section below for details on date formatting.
FQL | $fromMillis(1539387540000, "[Y]-[M]-[D] [H]:[m]:[s] [z]") |
Result | "2018-10-12 23:39:00 GMT+00:00" |
The $year()
, $month()
, and $day()
functions return their respective components from a year-month-day date format.
FQL | $year("2023-02-11") & "-" & $month("2023-02-11") & "-" & $day("2023-02-11") |
Result | "2023-2-11" |
The $hours()
, $minutes()
, $seconds()
, and $milliseconds()
functions return their respective values from a given date. The example below uses the $now()
function for the date.
FQL | $hours($now()) & ":" & $minutes($now()) & ":" & $seconds($now()) & ":" & $milliSeconds($now()) |
Result | "19:23:8:143" |
The $dayOfTheWeek()
function accepts a date and returns a number corresponding to a day of the week.
FQL | $dayOfTheWeek($now()) |
Result | 2 |
Number | Day |
---|---|
0 | Sunday |
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
The $hasSameDate()
function accepts two or more dates with parameters specifying values in the dates. The function compares the values specified by the parameters and returns true
if they're the same, or false
if they're not.
FQL | $hasSameDate("2023-02-01", "2023-02-08", ["month", "year"]) |
Result | true |
The $datePlus()
function accepts a date (formatted as YYYY-MM-DD
or milliseconds since epoch), the number of units you want to add, and the date component you want to advance (years
, months
, days
, hours
, minutes
, seconds
, or milliseconds
). The function returns the increased date as milliseconds since epoch.
FQL | $datePlus("2023-02-07", 1, "days") |
Result | 1675814400000 |
The $diffDate()
function accepts two dates (formatted as YYYY-MM-DD
or milliseconds since epoch) and a component of each date (years
, months
, days
, hours
, minutes
, seconds
, or milliseconds
), then returns the difference between the two dates' component.
FQL | $diffDate("2023-02-08", "2023-01-22", "days") |
Result | 17 |
Character | Meaning |
---|---|
Y | year |
M | month as a numerical value |
D | day in month |
d | day in year |
F | day of week |
W | week in year |
w | week in month |
H | hour (24 hours) |
h | hour (12 hours) |
P | am/pm marker |
m | minute |
s | second |
f | fractional seconds |
Z | timezone |
z | timezone but modified where to include a prefix as a time offset using GMT |
C | calendar: the name or abbreviation of a calendar name |
E | era: the name of a baseline for the numbering of years, for example the reign of a monarch |
The $append()
function can combine two arrays, an array and a single value, or two strings into an array.
FQL | $append([1,2,3], [4,5,6]) |
Result | [1,2,3,4,5,6] |
The $boolean
value is a true/false test. The second value is the output for true and the final value is the output for false. The example below tests if the total_value
field's value is greater than 250. Since the value is greater than 250, the function returns "high-value customer".
FQL | $boolean(customer_info.total_value > 250) ? "high-value customer" : "not a high-value customer" |
Result | "high-value customer" |
The $partition()
function breaks an array up into smaller arrays and returns the smaller arrays as a list.
FQL | $partition(payments,2) |
Result |
[ [{"invoice_number": "101301","date": "2022-09-11T16:12:34.494Z","description": "recurring subscription","amount": 110.48},{"invoice_number": "101302","date": "2022-09-29T14:45:13.148Z","description": "one time purchase","amount": 24.49}], [{"invoice_number": "101303","date": "2022-10-11T16:12:34.683Z","description": "recurring subscription","amount": 110.48},{"invoice_number": "101304","date": "2022-10-12T11:45:22.182Z","description": "recurring subscription deluxe","amount": 35.56}] ] |
In this example, the $map()
function gets the numerical values from the amount
fields in the payments
array and converts them to strings with the $string()
function.
FQL | $map(input.payments[].amount,$string) |
Result | ["110.48", "24.49", "110.48", "35.56"] |
In this example, the $filter()
function returns values greater than 40 from amount
fields in the payments
array.
FQL | $filter(input.payments[].amount,fn($v,$i,$a) { $v > 40}) |
Result | [110.48, 110.48] |
In this example, the filter()
function returns items from the list where the description
property matches the payments
variable.
FQL | list.payments.filter( (payment) => payment.description === payments ); |
Result | [ { "invoice_number": "101301", "date": "2022-09-11T16:12:34.494Z", "description": "recurring subscription", "amount": 110.48 }, { "invoice_number": "101303", "date": "2022-10-11T16:12:34.683Z", "description": "recurring subscription", "amount": 110.48 } ] |
The reduce()
function applies a function to every element in the array. In this example, it adds all the elements of the array together.
FQL | $reduce(input.payments[].amount,fn($i, $j){$i + $j}) |
Result | 281.01 |
The $sort()
function sorts an array by the function specified where $j
is the current item and $i
is the next item.
FQL | $sort(input.payments[].amount,fn($i, $j){$i < $j}) |
Result | [110.48, 110.48, 35.56, 24.49] |
In this example, the $each()
function converts every string value in an object to uppercase.
FQL | $each({"transaction_id": "inv_80394", "description": "buying 20 units of data"},$uppercase) |
Result | ["INV_80394", "BUYING 20 UNITS OF DATA"] |
Last modified: 2024/06/05
Additional resources
Videos
Blog posts