cancel
Showing results for 
Search instead for 
Did you mean: 

Question for javascript

walkerline117
Contributor

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

6 REPLIES 6

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?