Search code examples
jmeterjsr223post-processor

Failed to create new JsonObject using JSR223 PostProcessor in Jmeter using ResponseData


I'm trying to create a new Request Payload by capturing and amending the Response from previous sampler. I'm using JSR223 PostProcessor to write the code to create the payload. But lately, it's failing to create the script and throwing below error:

ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script15.groovy: 23: unable to resolve class JsonObject
 @ line 23, column 12.
   JsonObject response = JsonObject.readFrom(jsonString);

Here's my script:

import org.apache.jmeter.samplers.*;
import com.eclipsesource.json.*;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import java.math.BigInteger;

String jsonString = prev.getResponseDataAsString();

JsonObject response = JsonObject.readFrom(jsonString);
response.remove('reference');
response.remove('dateCreated');
response.remove('lastUpdated');
response.set("status","INACTIVE");
vars.putObject("newPayload",response.toString());

Here's what I've tried to create new "response", with no luck:

JSONObject response = new JSONObject.(jsonString);

Thank you in advance for the help!


Solution

  • My expectation is that you need to add the library providing this class (presumably minimal-json) to JMeter Classpath. JMeter restart will be required to pick the .jar up.

    However you don't even need any 3rd-party libraries, Groovy supports JSON language out of the box, see Parsing and producing JSON documentation chapter for more information on handling JSON in Groovy in particular and Apache Groovy: What Is Groovy Used For? article to learn about using Groovy in JMeter in general.