Skip to main content

Introduction

Transformers are JSON value spec templates.

It is used to transform multiple inputs (by spec) to a result object.

The most basic input to a transformer is $. All references to it can use JsonPath syntax.

Context​

Starts with #, inside transformers:

  • #now - ISO 8601 Timestamp as string
  • #uuid - Random UUID as string (with hyphens)
  • #null - Has the value null useful when used together with spread to remove a value
note

You can add additional context by supplying a Map<String, Object> to the transform function.

JsonPath functions​

JsonPath functions are available for use (as defined in json-path/JsonPath)

Some examples (from manual):

FunctionDescriptionOutput type
min()Provides the min value of an array of numbersDouble
max()Provides the max value of an array of numbersDouble
avg()Provides the average value of an array of numbersDouble
stddev()Provides the standard deviation value of an array of numbersDouble
length()Provides the length of an arrayInteger
sum()Provides the sum value of an array of numbersDouble
keys()Provides the property keys (An alternative for terminal tilde ~)Set<E>
concat(X)Provides a concatenated version of the path output with a new itemlike input
append(X)add an item to the json path output arraylike input

Inline Functions​

Inline functions can be used as prefix to values (referenced by strings):

  • Functions are prefixes for a path or a value in the format $$func:{value}
    • ending with just : will result with empty string as input "" (omitting : will result in null)
    • Functions can be piped to other functions as values
  • Functions may have arguments
    • Specified after the function name between parenthesis (i.e. $$func(arg1,arg2):{value})
    • Arguments can be optional/required (parenthesis can be omitted if no arguments at all)
    • All arguments can be quoted with (') single quote, escape it by writing it twice '' (e.g. $$wrap('don''t '):{value})
    • All arguments can be paths to other variables (e.g. $$func($.somepath,#current):$.somevalue)
    • Enum argument values are case-insensitive
    • Specifying Path/String/Enum arguments can be done without quotes
      • Enum type values are detected even with spaces around them (trimmed)
      • However, String & Path type values will not be trimmed (e.g. arg2 of $$func(1, hi):{value} will be processed as " hi"). To allow spaces between this kind of arguments, the values needs to be quoted (e.g. $$func(1, 'hi'):{value})

Object functions​

There are special functions that support more complex arguments.

  • These functions are defined by putting an Object value with a "function key" (starts with "$$").
  • The primary argument is the value of the function key. The rest of the arguments may be supplied as other keys in the same object.

Context​

Object functions also support nested function context

Examples​

Input

Definition

Output

{ "hello": "world" }
"$.hello"
"world"
{ "hello": "world" }
{ "x": "$.hello" }
{ "x": "world"}
{ "d": 13.333 }
"$$long:$.d"
13
[1, 2, 3, 4]
"$.length()"
4
{ "a": [ "b", { "c": "d" } ] }
"$.a[1].c"
"d"
[ {"a": 1}, {"a": 2} ]
"$.*.a"
[1, 2]
"text"
"$$join(s):$$split(x):$"
"test"
{ "x": "text"}
"$$wrap(>):$$substring(1,3):$.x"
">ex"