For detailed information see the xtrasonnet docs.
xtrasonnet is an extensible, jsonnet-based, data transformation engine for Java or any JVM-based language.
xtrasonnet is an extension of databricks' sjsonnet, a Scala implementation of Google's jsonnet. xtrasonnet enables extensibility, adds support for data formats other than JSON, and adds data transformation facilities through the xtr
library and some additions to the jsonnet language itself.
String output = new Transformer(xtrasonnet).transform(input);
input | xtrasonnet | output |
|
|
|
xtrasonnet has two points of extensibility:
- Custom functions: users can write native (e.g.: Java or Scala) functions as a
Library
and utilize them from their transformation code. - Any* data format: users can write a custom
DataFormatPlugin
and transform from/to a given data format.
* Any format that can be expressed as jsonnet elements.
xtrasonnet includes a DataFormatPlugin
for each of the following:
- JSON (application/json)
- XML (application/xml)
- CSV (application/csv)
- Java (application/x-java-object)
- text/plain
There are two main additions motivated to facilitate data transformation applications:
This allows developers to select, and chain, properties arbitrarily without testing existence.
xtrasonnet | Output |
local myObj = {
keyA: { first: { second: 'value' } },
keyB: { first: { } }
};
{
a: myObj?.keyA?.first?.second,
b: myObj?.keyB?.first?.second,
c: myObj?.keyC?.first?.second
} |
{
a: 'value',
b: null,
c: null
} |
This allows developers to tersely test for null
and provide a default value. For example
xtrasonnet | Output |
local myObj = {
keyA: { first: { second: 'value' } },
keyB: { first: { } }
};
{
a: myObj?.keyA?.first?.second,
b: myObj?.keyB?.first?.second ?? 'defaultB',
c: myObj?.keyC?.first?.second ?? 'defaultC'
} |
{
a: 'value',
b: 'defaultB',
c: 'defaultC'
} |
For a full reference see the xtr
docs.
The xtr
library is written natively (vs written as jsonnet code) and provides an extensive set of functions.
Included are slight variations of the general purpose functions found in the jsonnet's std
library, such as map
, filter
, and flatpMap
plus some additional ones like groupBy
. More specific functions are also included like objectFrom[Array]
to facilitate composing an Object
from an Array
, and orderBy
to sort elements.
but wait, there's more!
Developers will also find functions grouped by following set of modules, accessed in the form of xtr.[module].[function]
:
datetime
: operations on date and time values, likecompare
andplus(duration)
crypto
: encrypt, decrypt, or hash dataarrays
: extended set of array operations, likedistinctBy
andpartition
objects
: extended set of object operations, likeleftJoin
strings
: operations on strings, liketruncate
andwrapIfMissing
base64
: encode and decode data in base64url
: encode and decode data for URLs
and a few more.