How to pass Javascript Object from Java to Javascript using Nashorn

Viewed 4285

I have a JavaScript object in String form (actually coming from database) which I need to pass to a Javascript function using Nashorn (Java 8). The Engine treats the parameter passed as a string in javascript. I want it to identify it as a Javascript object.

Below is the code snippet:

String script = "function genData(dataModel) { return 'hello world '+ dataModel.url.value + ' done'; }";  

//"{url : {value : "abc.com",type  : "string"},layout : {value : "",type  : "string"}}";  

String dataModel = "{url : {value : \"abc.com\",type  : \"string\"},layout : {value : \"\",type  : \"string\"}}";

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Invocable inv = (Invocable) engine;
result = inv.invokeFunction("genData", dataModel);

The error I get is:
javax.script.ScriptException: TypeError: Cannot read property "value" from undefined in at line number 1

2 Answers
Related