Nashorn Engine Not resolving Params

Here is my code:

    JSONArray jsonArray = databaseQueryStatement.getJsonObject(query, connection);
    String x= "item1has quantity of queryResult[0].quantity";

    ScriptEngineManager manager  = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("nashorn");
    Bindings params = engine.createBindings();

    params.put("jsonArrayKey", jsonArray);
    params.put("c", x);
    String script = "var queryResult = JSON.parse(jsonArrayKey);"
                  + "var resultedString = c";

    String result=(String) params.get("resultedString");

When I pass the value queryResult[0].quantity instead of c it is giving me 2. But when I pass c it is printing:

item1has quantity of queryResult[0].quantity

I need to print:

item1has quantity of 2

How can I do this? I am using java and nashorn engine here.

The string that you put in bindings can’t be evaluated as script code because it is simply a literal.

Although I haven’t clear which is your intent if you change your code as follow then it prints the content as you expect:

String x= "item1 has quantity of ";
String script = "var queryResult = JSON.parse(jsonArrayKey);"
              + "var resultedString = c + queryResult[0].quantity";

For example a more flexible solution could be the following:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Bindings params = engine.createBindings();
params.put("jsonArrayKey", "[{/"quantity/":2},{/"quantity/":3}]");
params.put("msg", "item <ID> has quantity of <VALUE>");
params.put("index", 0);
String script = "var queryResult = JSON.parse(jsonArrayKey);/n"
                + "var resultedString = msg.replace('<ID>', index + 1).replace('<VALUE>', queryResult[index].quantity);";