如何解决WSO2 EI v6.4.0中‘jsonPath is not defined’错误
I totally get the frustration of hitting that "jsonPath is not defined" error when trying to use jsonPath in your WSO2 EI JS templates—let’s walk through how to fix this step by step.
Why You’re Seeing This Error
WSO2 EI uses the Rhino JavaScript engine, which doesn’t include a global jsonPath function out of the box. Adding the JAR files alone isn’t enough because you need to properly reference the Java-based jsonPath library within your JS code, rather than trying to call it like a native JS function. Also, mixing lib and dropins directories can cause classloading conflicts, and groovy-all isn’t necessary for JS-based jsonPath usage.
Step-by-Step Solutions
1. Fix Dependency Placement & Clean Up
- Move
json-path-2.4.0.jarand all its required dependencies (likejson-smart-2.3.jar,slf4j-api-1.7.25.jar—check the json-path Maven repo for full dependencies) to onlyWSO2_HOME/libdirectory. Remove them fromdropins(dropins is for OSGi bundles, not standard JARs). - Remove
groovy-all-2.4.4.jarunless you’re explicitly using Groovy scripts—you don’t need it for JS jsonPath operations. - Restart your WSO2 EI server to ensure the JARs are loaded correctly.
2. Correctly Call jsonPath in Your JS Code
Instead of trying to use a global jsonPath function, you need to invoke the Java JsonPath class directly via Rhino’s Java interop features. Modify your test function like this:
function denemeJsonPath(){ var newPayload = {"store":{"book":[{"searchTerm":"AA","country":"BB","language":"CC"},{"searchTerm":"DD","country":"EE","language":"FF"},{"searchTerm":"GG","country":"HH","language":"ZZ"}]}}; // Import the JsonPath Java class var JsonPath = Java.type("com.jayway.jsonpath.JsonPath"); // Convert JS object to JSON string for Java's JsonPath var payloadStr = JSON.stringify(newPayload); // Execute the jsonPath query var result = JsonPath.read(payloadStr, "$..book[2]"); // Convert Java result back to a JSON string for printing var JSONObject = Java.type("org.json.JSONObject"); print("responseeeeeeeeeeeeeeeeeeeeeeeeeee:" + new JSONObject(result).toString()); }
Let me break this down:
Java.type()is Rhino’s way to access Java classes from JS.- We convert the JS object to a JSON string because Java’s JsonPath works best with string inputs here.
- The result from
JsonPath.read()is a Java object, so we use WSO2’s built-inorg.json.JSONObjectto convert it back to a readable JSON string.
3. Alternative: Use Native JS for Simple Queries
For straightforward path selections (like your example), you can skip the external library entirely and use native JS:
function denemeJsonPath(){ var newPayload = {"store":{"book":[{"searchTerm":"AA","country":"BB","language":"CC"},{"searchTerm":"DD","country":"EE","language":"FF"},{"searchTerm":"GG","country":"HH","language":"ZZ"}]}}; var book2 = newPayload.store.book[2]; print("responseeeeeeeeeeeeeeeeeeeeeeeeeee:" + JSON.stringify(book2)); }
This is lighter and avoids external dependencies for simple use cases.
Verify the Fix
- Deploy your updated JS template to WSO2 EI.
- Trigger the function and check the server console—you should see the expected JSON output instead of the error.
内容的提问来源于stack exchange,提问作者AnilF

