โ02-14-2018 03:53 AM
Hi Everyone,
I have written a simple javascript code in Script snap. Please find the code below:
script = {
execute : function()
{
while (input.hasNext())
{
var test = [โaโ, โbโ];
try
{
output.write(test);
}
catch(err)
{
new_data.errMsg = err.message;
new_data.tryBlock = "failed";
error.write(new_data);
}
}
}
};
var hook = new com.snaplogic.scripting.language.ScriptHook(script)
I am basically just creating an array with values [โaโ, โbโ] and printing it. Please find the output below:
But in the output, you can see that it has become an object {0:a, 1:b} with keys as โ0โ and โ1โ. Please help me with this. How can I get an array [โaโ, โbโ] as the output?
โ02-14-2018 10:32 AM
I think you are making TWO mistakes! I had this same problem only about a week ago, and solved it with the following python code:
PYTHON SCRIPT
in_doc[โKEYSโ]= json.loads( ln )
output.write(in_doc)
ln is the array
HEADER CHANGE to the python script:
from com.snaplogic.scripting.language import ScriptHook
import java.util
import json
from pprint import pprint
So I can hook a json spliter snap onto the script snap, and process each line coming from the snap. The intent was to reprocess data from a reltio load error in snaplogic, and it gets the detail from the payload in the error. The payload could have dozens of entities represented in the one payload.
There is probably a similar problem with jscript.
Steve
โ02-14-2018 07:25 PM
You need to define arraylist.
I often get confused between datatypes when using python on SnapLogic which is jython.
I am assuming javascript is also using JSR engine which is different than traditional javascript.
If it is possible I would highly recommend using python script instead of JavaScript.
// Ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines
try { load(โnashorn:mozilla_compat.jsโ); } catch(e) { }// Import the interface required by the Script snap.
importPackage(com.snaplogic.scripting.language);// Import the Java utility classes.
importPackage(java.util);/**
Create an object that implements the methods defined by the โScriptHookโ
interface. Weโll be passing this object to the constructor for the
ScriptHook interface.
/
var impl = {
/
- These variables (input, output, error, log) are defined by the
- ExecuteScript snap when evaluating this script.
*/
input : input,
output : output,
error : error,
log : log,/**
The โexecute()โ method is called once when the pipeline is started
and allowed to process its inputs or just send data to its outputs.
Exceptions are automatically caught and sent to the error view.
*/
execute : function () {
this.log.info(โExecuting Transform Scriptโ);
while (this.input.hasNext()) {
try{
// Read the next document, wrap it in a map and write out the wrapper
var doc = this.input.next();
var wrapper = new java.util.HashMap();
var out = new java.util.ArrayList();
out.add(โaโ)
out.add(โbโ)
this.output.write(doc, out);
this.log.info(โTransform Script finishedโ);} catch(err) { var wrapper = new java.util.HashMap(); wrapper.put("errorMsg", err); this.log.error(err); this.error.write(wrapper); }
}
}
};/**
- The Script Snap will look for a ScriptHook object in the โhookโ
- variable. The snap will then call the hookโs โexecuteโ method.
*/
var hook = new com.snaplogic.scripting.language.ScriptHook(impl);
Happy coding!
โ02-14-2018 11:12 PM
Thank You so much. It helped. ๐