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.
Packages​
| Language | Name | Description | License | Status |
|---|---|---|---|---|
| Java | co.nlighten.json-transform | Java library for transforming JSON objects | Apache License 2.0 | |
| JavaScript | @nlighten/json-transform | JSON transformers JavaScript implementation | MIT | |
| JavaScript | @nlighten/json-transform-core | Core types and utilities for handling JSON transformers | MIT | |
| JavaScript | @nlighten/monaco-json-transform | Monaco editor extension for JSON transformers | MIT |
Context​
Starts with #, inside transformers:
#now- ISO 8601 Timestamp as string#uuid- Random UUID as string (with hyphens)#null- Has the valuenulluseful 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):
| Function | Description | Output type |
|---|---|---|
| min() | Provides the min value of an array of numbers | Double |
| max() | Provides the max value of an array of numbers | Double |
| avg() | Provides the average value of an array of numbers | Double |
| stddev() | Provides the standard deviation value of an array of numbers | Double |
| length() | Provides the length of an array | Integer |
| sum() | Provides the sum value of an array of numbers | Double |
| 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 item | like input |
| append(X) | add an item to the json path output array | like 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 innull) - Functions can be piped to other functions as values
- ending with just
- 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) Enumargument values are case-insensitive- Specifying
Path/String/Enumarguments can be done without quotesEnumtype values are detected even with spaces around them (trimmed)- However,
String&Pathtype 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})
- Specified after the function name between parenthesis (i.e.
Object functions​
There are special functions that support more complex arguments.
- These functions are defined by putting an
Objectvalue 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"