Custom functions
Creating and using custom functions
Custom functions in Create are pieces of JMESPath code with defined input parameters. They are useful for simplifying complex transformations that you need to reuse across multiple transformations within a unit.
Overview
A custom function is a named transformation that accepts zero or more parameters. When called, the function evaluates its parameters and returns the result of the transformation defined in the function body.
They are created in an editor specifically made for data transformations.
Custom functions are useful when:
- A transformation is repeated across recipes
- A calculation or formatting rule should be centralized
- A business rule needs a clear and maintainable implementation
- A transformation becomes too complex to write inline
Custom functions can only be used inside the unit they are created in.
Function structure
A custom function contains three core elements:
| Element | Description |
|---|---|
| Function name | The identifier used when calling the function. |
| Parameters | Optional named inputs that can be passed by value or expression. |
| Transformation | A JMESPath expression that can use the optional parameters to produce a result. |
Naming rules
Function names must follow these rules:
- The name must start with a letter or an underscore.
- The name may contain letters, numbers and underscore.
- Spaces or special characters are not allowed.
Parameters
A custom function can define any number of parameters. Parameters control how input is passed into the function and how that input is evaluated.
Each parameter has:
- A name
- A passing mode
Parameter passing modes
There are two parameter modes: By value and By expression.
By value
When a parameter is passed by value, the argument is evaluated when the function is called. The result of that evaluation is passed into the function.
Behavior:
- Evaluation happens before entering the function.
- The parameter holds a final value, not an expression.
- Useful for simple inputs such as strings, numbers or direct lookups.
Example:
#discountedPrice(price)By expression
When a parameter is passed by expression, the argument is not evaluated when the function is called. Instead, the expression is evaluated inside the function body, in the current scope at the time it is accessed.
Behavior:
- Evaluation happens inside the function.
- Useful for dynamic lookups that depend on the function’s internal logic.
- Allows functions to reference data relative to internal contexts.
Example:
#isLargeOrder(items)Default mode
All parameters are By value by default unless explicitly switched to By expression in the function configuration.
Function transformation
The transformation is a JMESPath expression that uses parameter names prefixed with # to reference the parameter values.
Example:
{
fullName: join(' ', [#firstName, #lastName])
}You can use any valid combination of JMESPath operators, functions, arrays, objects or expressions inside the transformation.
Calling a function
Once a function is defined, it can be used anywhere within the same unit by referencing it by its name preceded by a hashtag.
Example: #myCustomFunction().
Custom functions are used for centralizing transformations in order to reuse them in multiple locations.
Updated 4 days ago