10-31-2017 07:46 PM
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
10-31-2017 08:09 PM
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 .
10-31-2017 09:57 PM
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 nativejava.util.Map
orjava.util.List
implementations, such asHashMap
andArrayList
.
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.
11-01-2017 06:12 AM
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
11-01-2017 06:31 AM
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.