Question for javascript

Hi

Suppose i have below json.

{
    "id":"1",
     "name":"name1"
    "age": 5
}

I want to use javascript to add an additional field “schools” into above json to be like

{
    "id":"1",
     "name":"name1"
    "age": 5,
    "schools":[
      {
         "school_id": 1,
         "school_name":"name1"
       },
      {
          "school_id": 2,
         "school_name":"name2"
      }
     ]
}

I tried to use javascript to add that schools field into the json.
So I have

while (this.input.hasNext()) {


var doc = this.input.next();
var wrapper = new java.util.HashMap();

var newObj=[];

var newSchool1={};
newSchool1.school_id=1;
newSchool1.school_name="name1";
newObj.push(newSchool1);

var newSchool2={};
newSchool2.school_id=2;
newSchool2.school_name="name2";
newObj.push(newSchool2);

doc.schools=newObj;
 wrapper.put("original", doc);
 this.output.write(doc, wrapper);
}

However, the result from above code is like below

{
    "id":"1",
     "name":"name1"
    "age": 5,
    "schools":{
      "0":{
         "school_id": 1,
         "school_name":"name1"
       },
      "1":{
          "school_id": 2,
         "school_name":"name2"
      }
     }
}

Am I missing anything?

Thanks

it seems Snaplogic take [] in javascript as {}

So if I create an object var obj=[] and append this obj to a json, it shows {} instead of [].

Hi, as noted in the Script Snap documentation:

Important Notes
The SnapLogic platform (when running the pipeline) can not deal with script-language-specific container types, such as lists and arrays. Those need be represented explicitly by using java native java.util.Map or java.util.List implementations, such as HashMap and ArrayList.

There are further examples listed in the link above. Also, if using the Script Snap is not a necessity, there are ways to use Transform Snap Pack Snaps to group/aggregate multiple document objects together into an array and then add it as a field (e.g. via the Mapper) to an existing object.

Hi, Robin

Yes, i understand i can use groupby and aggregate and using script snap is not a good solution at all.
However, in our case, we are building Ultra pipeline in which groupby or aggregate will not work.

We are building a lot of time sensitive APIs in our company, so Ultra task is the only way to fullfill our requirement.

I will take a look at the java script library you mentioned, some how, the documentation does not provide enough examples, that should be one of the improvement you guys need to work on.

Thanks

Please try to move your group by and aggregates to a child pipeline and call them using pipeline execute. as long as a single element is returned to the ultra level, it should work. Hope that makes sense.

Hi,

So basically we are migrating our old ESB process to snaplogic, which mostly are APIs.
Though moving groupby or aggregate snap into child pipeline will work, but performance is worse than script snap as we have compare with many process and pipelines.

Same suggested by Snaplogic professional, a script snap is need in such complected transformation in Ultra task.

By using child pipeline in ultra or use triggered task with our API, the performance is way worse than the API developed in our traditional ESB. However ultra would gain at least the same performance, but no child pipeline.

Thanks

Did you get this resolved?