Skip to content

Call functions

MACK Mathieu edited this page Jan 24, 2022 · 1 revision

This page describes how to call functions during your transformation.

Call a function

A function can be called by using the syntax : '[source]->Function name

  • [source] is optional and depends on the function implementation.
  • [Function name] is mandatory and must contains the name of the function without spaces.
  • [parameters] is optional. If you have many parameters, you must separate it with a coma char. Do not include spaces around coma. Ex :
{
    "complete": "$.sourceSelector->MyFunction($.selector1,parameter 2 hard coded)"
}

Ex :

{
    "complete": "->MyFunction()"
}

Functions can be called on fields, or on values, depending on the function execution.

Ex :

{
    "hello" : {
        "$.values->loop(entry)": {
            "field" : "$.entry"
        }
    }
}

Register custom functions

The JsonTransform class can take a list of custom operations in the constructor :

/// <summary>
/// Create the transformation engine with custom functions
/// </summary>
/// <param name="conditionalOperations">List of custom function that returns a true/false result. Can be used for if/else if for ex.</param>
/// <param name="customOperations">List of custom functions that apply a transformation on a json content. Convert to integer, ...</param>
public JsonTransform(IEnumerable<IJsonTransformConditionalCustomOperation> conditionalOperations,
                        IEnumerable<IJsonTransformCustomOperation> customOperations);

Embeded functions

The SDK contains some functions that can be used by default on your application. These functions can be registered on your ServiceCollection by using the extension method 'RegisterJsonCustomTransformFunctions' on your startup.cs class. Ex :

using JSLTSharp.JsonTransforms.Extensions;
...
    serviceCollection.RegisterJsonCustomTransformFunctions();
...

Transform values operations

ConcatString

This method concat some string that can be hard coded strings, or selectors from the json input : Input :

{
    "start": "sample",
    "end": "value result"
}

Transformation :

{
    "complete": "->ConcatString($.start,-,$.end)"
}

Output :

{
    "complete": "sample-value result"
}

ToBoolean

This method transform a value to a boolean (true/false) : Input :

{
    "field": 1
}

Transformation :

{
    "result": "$.field->ToBoolean()"
}

Output :

{
    "result": true
}

ToInteger

This method transform a value to an integer : Input :

{
    "field": 153.35
}

Transformation :

{
    "result": "$.field->ToInteger()"
}

Output :

{
    "result": 153
}

ToUpper

This method transform a value to uppercase value : Input :

{
    "field": "hello"
}

Transformation :

{
    "result": "$.field->ToUpper()"
}

Output :

{
    "result": "HELLO"
}

FormatDate

This method transform a date with a specific pattern as output. This method called the ToString: Input :

{
    "field": "01/01/2022"
}

Transformation :

{
    "result": "$.field->FormatDate(s)"
}

Output :

{
    "result": "2022-01-01T00:00:00"
}

Distinct

This method, applied on an array, keeps unique elements: Input :

{
    "fields": [ "1", "2", "3", "1", "3" ]
}

Transformation :

{
    "result": "$.fields->Distinct()"
}

Output :

{
    "result": [ "1", "2", "3" ]
}

Conditional operations

Create your own functions

You can create your own function to transform your data by implementing a class that implements the interface IJsonTransformCustomOperation. For conditional operations, you must create a class that implements the interface IJsonTransformConditionalCustomOperation.

Data