Skip to main content

Using Variables in KurocoEdge – Variable Transformations

info

Estimated required time:15 min

Overview

Sometimes when working with advanced variables, business logic requires to transform these variables before being able to use them - for example transform an Array into a String using some join separator, instead of the default JSON representation. This is when Variable Transformations are useful.

What You Will Learn

How to use Variable Transformations

In all cases, Variable Transformations will create a new variable with the result of applied transformations. So it requires a Name - as always, it can be assigned to a nested variable (existing or not, in which case it will be created) using . notation.

Next, there is a Value input. There are 2 modes for the value, or value interpretations : this dictates what is expected in the Value field, and how should the system understand it.

Value InterpretationExplanationInput variable typeExamples
String ExpressionIn this mode, the value is evaluated as a custom string. For example the user can statically define a JSON string from scratch (for example an array or object). But most importantly, this allows to use {$existing_variable} notation.In all cases the input will be a stringMy {$custom_data} or {"my_object_key": "{$captured_value}"}
Variable PointerIn this mode, an existing variable is directly pointed at. You may only input target variable location (nested if necessary), and you cannot combine multiple variables. The pointed variable will not be modified, only read - the transformations applied will create a new one. Also, note that neither brackets nor $ sign are usedSame as the pointed variable's typeexisting_var or if nested existing.nested.var

After the input is specified, it is time to apply one or more functions.

Functions Overview

Any number of functions may be applied in a chain, in order of definition (top to bottom).

caution

KurocoEdge enforces strict type system for the variables. It is important to pick the function which is applicable on the given type. Per convention, Functions names begin with a type indicating the applicable input type.

In case of an error (due to pointed variable not existing, or function type mismatch), this Action will be nulled and an error logged - as always, check the KurocoEdge response Log header for more information.

info

In Preview mode, for Rule development convenience, any runtime error is displayed inside the Preview modal.

Currently the following functions are currently defined :

Function NameDescriptionResulting type
StringParseToJsonParses an input string as JSONWhatever type the input JSON string is
ArrayJoinTransforms an Array into a String by concatenating each element with the given SeparatorString
ObjectKeysTransforms an Object into an Array using its keysArray
ObjectValuesTransforms an Object into an Array using its valuesArray
info

The list of available functions is being kept minimal on purpose, and is expanded based on user needs. Therefore, if there is a function you think would be useful for your business logic, consider applying an inquiry through KurocoEdge support channels.

Examples

Let's say a Variables API call returns the following object:

{
"cities": {
"Tokyo": {
"region": "Kanto",
"pop": "14 millions"
},
"Osaka": {
"region": "Kinki",
"pop": "2.7 millions"
}
}
}

And suppose you specify cities as Expected Variable. Therefore, cities is now a JSON Object in KurocoEdge memory for the request. You can then extract the City Names list using the following setup :

Image from Gyazo

This will create a String variable called city_names with value Tokyo,Osaka


Support

If you have any other questions, please contact us or check out Our Discord Community.