Skip to content

extensible jsonnet transformations

xtrasonnet is an extensible, jsonnet-based, data transformation engine for Java or any JVM-based language.


xtrasonnet

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.

{
    "message": "hello, world!"
}
/** xtrasonnet
input payload application/json
output application/xml
*/
{
    root: {
        msg: payload.message,
        at: xtr.datetime.now()
    }
}
<?xml version='1.0' encoding='UTF-8'?>
<root>
    <msg>hello, world!</msg>
    <at>2022-08-14T00:19:35.731362Z</at>
</root>

How extensible?

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.

What kind of additions to the jsonnet language?

There are two main additions motivated to facilitate data transformation applications:

Null-safe select ?.

This allows developers to select, and chain, properties arbitrarily without testing existence.

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
}

Null coalescing operator ??

This allows developers to tersely test for null and provide a default value. For example

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'
}