Skip to content

Composing transformers

When a transformer grows too large to reason about in one file, split it into several and call them from each other with #transformWith. It takes a path to the sub-document to transform and the name of a registered transformer.

You need a name and the transformer JSON:

var registration = new TransformerRegistration("PartialTransformer", partialTransformerJson);
context.RegisterTransformer(registration);

In a property value of the primary transformer:

{
"part": "#transformWith($.some.path, 'PartialTransformer')"
}

This runs the registered transformer against the sub-document at $.some.path and uses its output as the value.

Variables from the calling transformer aren’t visible in the called one. Instead, pass a JSON object as a third argument. It’s stored in an implicit variable called @params.

primary transformer
{
"formattedDate": "#transformWith($.date, 'DateFormatter', { 'format': 'MM/dd/yyyy' })"
}
DateFormatter
{
"date": "#formatDateTime($.some.date, @params.format)"
}

When no parameters are passed, @params is an empty object rather than null. It’s local to that transformer’s execution.

Use #merge to add context-specific properties to a focused, reusable transformer’s output:

{
"enrichedResult": "#merge(#transformWith($.some.path, 'PartialTransformer'), { 'source': 'local', 'processedAt': #currentDateTimeUtc() })"
}
{
"#foreach(@x in $.order.items) into 'lineItems'": [
{
"item": "#transformWith(@x, 'LineItemTransformer', { 'currency': #valueOf($.order.currency) })"
}
]
}

Validate() checks that every #transformWith call refers to a registered transformer.