Although this question was asked long ago, the answers provided still hold true. I wanted to share my solution in case it can benefit others dealing with passing complex objects between Java and Javascript.
I developed a script that converts NativeObject to JSON objects stored in memory (specifically using MongoDB's BSON-based objects). You should be able to simply substitute JSONArray and JSONObject for them in the code sample below.
For instance, if you have a script called "create_object_script" that returns an object or array, you can convert it into JSON format (a list of hashmaps) like so:
Object returnVal = engine.eval(create_object_script);
engine.put("output", returnVal);
BasicDBObject objFactory = new BasicDBObject(); // (or equivalent in JSON)
BasicDBList listFactory = new BasicDBList(); // (or equivalent in JSON)
BasicDBList outList = new BasicDBList(); // (or equivalent in JSON)
engine.put("objFactory", objFactory);
engine.put("listFactory", listFactory);
engine.put("outList", outList);
engine.eval(parsing_script); // (explained below)
// "outList" now contains JSON representations of "returnVal" in memory
If you have control over the "create_object_script," you can streamline this process into a single step. Since my scripts are user-generated, hiding the complexity is necessary - users just need to ensure the "return value" is the final line of their script.
You can find the parsing_script gist here to keep this post concise.
This method works effectively for me; as I am not experienced in JS development, there may be more efficient ways to achieve this. Note that I always require results in a list format, but you could modify this approach by passing a BasicDBObject "outObj" and writing directly to it instead in singleton cases.
I hope this explanation proves helpful to anyone facing a similar challenge late at night!