Forum Discussion
I’m trying to solve the same problem, but for GitHub instead of GitLab. Did you ever resolve this?
I started with Python too and ran into a similar issue, but since this is Python 2 on Jython, it felt more prudent to stick with Javascript if I could. I’m very inexperienced in Javascript, but I was able to get a pretty-printed version of the JSON object, but only with the top-level keys sorted. I don’t know how to traverse the LinkedHashMap object that SnapLogic uses and convert it into a sorted TreeMap object.
// 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 serializable Java type we'll use for the output data.
importClass(java.util.LinkedHashMap);
importClass(java.util.TreeMap);
importClass(com.google.gson.Gson);
importClass(com.google.gson.GsonBuilder);
/**
* 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 input document, store it a new LinkedHashMap, and write this as an output document.
// We must use a serializable Java type liked LinkedHashMap for each output instead of a native
// JavaScript object so that downstream Snaps like Copy can process it correctly.
var inDoc = this.input.next();
var outDoc = new LinkedHashMap();
var inDocTree = new TreeMap();
inDocTree.putAll(inDoc)
gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create()
var json_pretty = gson.toJson(inDocTree, TreeMap.class);
outDoc.put("original", inDoc);
outDoc.put('inDocTree', inDocTree);
outDoc.put("json_pretty", json_pretty);
this.output.write(inDoc, outDoc);
}
catch (err) {
var errDoc = new LinkedHashMap();
errDoc.put("error", err);
this.log.error(err);
this.error.write(errDoc);
}
}
this.log.info("Script executed");
},
/**
* The "cleanup()" method is called after the snap has exited the execute() method
*/
cleanup : function () {
this.log.info("Cleaning up")
}
};
/**
* 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);
Sorry for the late reply, I was off for some time.
I ended up just dumping it to a sorted string and then parse the String, I’ve attached the pipeline, if you run into issues with your solution maybe you can fall back to this.
sortJSON_2021_07_19.slp (6.4 KB)
Best regards
Thomas