Scripter
8 years agoOccasional Contributor
parsing json via Groovy in soapui
The JSON Data is a response of a rest service(list_files)
{"Files": [ {"filepath": "input/file_29112017d.csv"}, {"filepath": "input/file_29112017d.log"}, {"filepath": "input/file_29112017d.ini"}, {"filepath": "output/file_29112017d.xml"}, {"filepath": "output/file_29112017d.csv.trc"} ]}
The goal is to get the path of csv file and not *csv.trc. Also the order of response varies with each test runs i.e
$.Files[*].filepath[0] does not yield always
input/file_29112017d.csv
This doesn't work either
import static com.jayway.jsonpath.JsonPath.parse def response = context.expand( '${list_files#Response}' ) //log.info response gives correctly def csvpath= parse(response).read('$.Files[?(@.filepath =~ /.*csv/i)]') log.info csvpath
the error is
Wed Nov 29 17:06:54 CET 2017:ERROR:java.lang.IllegalArgumentException: Unsupported operator = java.lang.IllegalArgumentException: Unsupported operator = at com.jayway.jsonpath.internal.filter.eval.ExpressionEvaluator.eval(ExpressionEvaluator.java:65) at com.jayway.jsonpath.internal.filter.ArrayEvalFilter.isMatch(ArrayEvalFilter.java:90) at com.jayway.jsonpath.internal.filter.ArrayEvalFilter.filter(ArrayEvalFilter.java:69) at com.jayway.jsonpath.internal.filter.PathTokenFilter.filter(PathTokenFilter.java:50) at com.jayway.jsonpath.JsonPath.read(JsonPath.java:255) at com.jayway.jsonpath.internal.JsonReader.read(JsonReader.java:103) at com.jayway.jsonpath.internal.JsonReader.read(JsonReader.java:97) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:189) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at Script12.run(Script12.groovy:5) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:90) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
Hi Scripter,
SoapUI 5.3.0 uses an older version of the JSONPath library (v0.9.1) which does not seem to support the regex match operator (=~).
Try JsonSlurper instead:
import groovy.json.JsonSlurper
...
def json = new JsonSlurper().parseText(response) def csvpath = json.Files.find { it.filepath.endsWith(".csv") }.filepath log.info csvpath