Foundations.JSON.Transformer

Transformer.initialize

Register a template containing a set of named transforms. This template is then used for all other operations.

Syntax

    void initialize(template object);

Parameters

Argument Required Type Description
template object Yes any JSON object containing set of named transforms.

Returns

None.

Example

var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"});
var fjson = jsonlib["foundations.json"];
var myTransformer = new fjson.Transformer();
var template =  { "user": { FirstName: "!{person.displayName}", LastName: "!{person.surname}", Country : "USA", State:"CA", Location: "!{person.building}" } };
myTransformer.initialize(template);


Transformer.merge

Merge two different JSON data objects.

Syntax

   merge (olddata, newdata);

Parameters

Argument Required Type Description
olddata Yes any JSON data object(s) that are going to be merged with the new objects.
newdata Yes any JSON data object(s) that are going to be merged with old data.

Returns

    {
        merged data
    }
Argument Required Type Description
merged data Yes any JSON data object(s)—old data object(s) merged with new data object(s).

Example

var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"});
var fjson = jsonlib["foundations.json"];
var myTransformer = new fjson.Transformer();
olddata = {"displayName": "Fred"};
newdata = {"street": "Main"};
Mojo.Log.info("FoundJSON: merge ="+ JSON.stringify(myTransformer.merge(olddata, newdata)));

Example Output

FoundJSON: merge ={"displayName":"Fred","street":"Main"},


Transformer.transform

Transform JSON data object(s) according to a set of named transforms in a registered template.

A simple template might look as follows:

{ "to": { name: "!{displayName}", type: "first" }

The !{...} indicates a jsonPath expression that should be evaluated for the given data.

Syntax

    transform (data);

Parameters

Argument Required Type Description
data Yes any JSON data object(s) that are going to be transformed given the set of named transforms in the last registered template.

Returns

    {
        transformed data
    }

Example

var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"});
var fjson = jsonlib["foundations.json"];
var myTransformer = new fjson.Transformer();
var template =  { "user": { FirstName: "!{person.displayName}", LastName: "!{person.surname}", Country : "USA", State:"CA", Location: "!{person.building}" } };
myTransformer.initialize(template);
var data = { "person": {"displayName" : "Fred", "surname" : "Flintstone", "building":"3-1" }};

Mojo.Log.info("FoundJSON: transform ="+ JSON.stringify(myTransformer.transform(data)));    

Example Output

FoundJSON: transform ={"user":{"FirstName":"Fred","LastName":"Flintstone","Country":"USA","State":"CA","Location":"3-1"}} 


Transformer.transformAndMerge

Transform the new data with the registered template and merge it with the old data.

Syntax

    transformAndMerge (olddata, newdata);

Parameters

Argument Required Type Description
olddata Yes any JSON data object(s) that are going to be merged with the new objects.
newdata Yes any JSON data object(s) that are going to be transformed (if template registered) then merged with old data.

Returns

    {
        merged data
    }
Argument Required Type Description
merged data Yes any JSON data object(s)—old data object(s) merged with new data object(s).

Example

var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"});
var fjson = jsonlib["foundations.json"];
var myTransformer = new fjson.Transformer();
var template = { "Address" : "!{street}"};
myTransformer.initialize(template);
olddata = {"displayName": "Fred"};
newdata = {"street": "Main"};
Mojo.Log.info("FoundJSON: transformAndmerge ="+ JSON.stringify(myTransformer.transformAndMerge(olddata, newdata)));    

Example Output

FoundJSON: transformAndMerge ={"displayName":"Fred","Address":"Main"},