I'm getting error while running the bellow java code in Jmeter JSR223 sampler
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;
String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";
Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
JSONArray authorsArr = JsonPath.using(config).parse(json).read(jsonPath);
System.out.println(authorsArr.get(0).toString());
json-path-2.4.0.jar is added under ..\lib\ext and assume it is automatically loaded on Jmeter start.
The above code is checked in IDE and is running fine.
JSR223 Error
2021-02-02 17:19:10,580 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_241]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
Change "Language" to groovy
, if you choose java
it isn't really Java, it's Beanshell which
So according to JMeter Best Practices you should switch to Groovy since JMeter 3.1. More information: Apache Groovy - Why and How You Should Use It
Change this line:
String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";
to this one
String jsonPath = '$..book[?(@.author == \'Nigel Rees\')].title'
as your Java syntax slightly conflicts with Groovy GString Template Engine
You should not require any extra .jars, the code will work on vanilla JMeter instance
Also be aware that Groovy has built-in JSON support