0
votes

I've the below JSON response received from the HTTP request. I want to extract the parameters from the node "url" present in the JSON response.

{"Id":"7S9LyBqyv1e0trKrVuP1OOZGHeg","Url":"https://abcd.com:443/u/custom-response?prov=34545sdf-9013e2e61e66&realmeId=%2Fxxxx","realmeId":"/abcd"}

In the above JSON response, i want to retrieve the value of "prov" which is 34545sdf-9013e2e61e66 using JMeter. Solution Tried: Used Beanshell to read the response.

String url = vars.get("successURL");
vars.put("responseURL",url.toString());
responseURL = responseURL.replaceAll(":"," ");
log.info("String URL "+responseURL.toString());

Error Message:

attempt to resolve method: toString() on undefined variable or class name: responseURL
2

2 Answers

2
votes

You don't need to use complex Beanshell coding, You can do this easily using - Regular Expression Extractor.

Here the example:-

enter image description here

You can see required value extracted and stored in variable name give in Regular expression extractor:

enter image description here

To understand try out reg-ex, you can use https://regex101.com/

2
votes

I think you need to update this line:

responseURL = responseURL.replaceAll(":"," ");

to something like:

responseURL = vars.get("responseURL").replaceAll(":"," ");

However I don't guarantee that it will work because I don't know how do you get this successURL variable.

What will work is doing everything in one shot using JSR223 PostProcessor and Groovy language which has built-in JSON support, suggested code:

def url = new groovy.json.JsonSlurper().parse(prev.getResponseData()).Url
def params = org.apache.http.client.utils.URLEncodedUtils.parse(new URI(url), 'UTF-8')
params.each { param ->
    log.info(param.getName() + '=' + param.getValue())
} 

Demo:

enter image description here

More information: Apache Groovy - Why and How You Should Use It