To print JMeter results in txt file

34 views
Skip to first unread message

Mohd Sarfraz

unread,
Aug 9, 2025, 12:51:22 PMAug 9
to jmeter-...@googlegroups.com
Hi Experts,

I have this JSR223 Listener with this code to print all the responses into a txt file.
image.png

I have this response in View Results Tree and there are too many like this, 
{
  "Responses": [
    {
      "P_name":"ABC",
      "P_id":"123",
      "P_Remarks":"Ok",
      "P_Description":"XYZ",
      "P_Status":"Success",
      "P_Response": " ", this is too long and i want to filter it out
      "P_date": "2025-08-09"
},
{
      "P_name":"ABC2",
      "P_id":"124",
      "P_Remarks":"Ok",
      "P_Description":"XYZ2",
      "P_Status":"Success",
      "P_Response": " ", this is too long and i want to filter it out
      "P_date": "2025-08-09"
}
  ]
}


Case-1
I want to print all the fields except "P_Response" but where "P_id":"123", & "P_id":"124", only selected ones
How can i achieve this?
Note: There are too many P_id in the response.


Case-2

I want to print the entire response into the txt file but do not want to print  "P_Response"  as this is too long and I want to filter it out from the txt file.
How can i achieve this?





Sincerely
Mohd Sarfraz


sebi...@gmail.com

unread,
Aug 9, 2025, 1:39:26 PMAug 9
to jmeter-plugins
Hello from the other side,
  First of all, you can use StringToFile function to put any string into any file
  To play with JSON you can use JSON JMESPath Extractor
  You can use Dummy Sampler and in Response put your text to be extracted with  JSON JMESPath Extractor

Screenshot From 2025-08-09 20-36-37.png


 Screenshot From 2025-08-09 20-38-32.png


Best regards,
Sebi

Shaik Salim

unread,
Aug 10, 2025, 1:12:39 AMAug 10
to jmeter-...@googlegroups.com
You need to use beanshell script for this I will work out and send it later

--
You received this message because you are subscribed to the Google Groups "jmeter-plugins" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jmeter-plugin...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jmeter-plugins/d84a87d9-0908-49fd-8dc4-3a87cbb8876an%40googlegroups.com.

Mohd Sarfraz

unread,
Aug 10, 2025, 1:34:10 AMAug 10
to jmeter-...@googlegroups.com
Thats what I am aware, here requirements are different based on given condition. 
I want to filter out the results wrt P_id 

Shaik Salim

unread,
Aug 10, 2025, 3:33:42 AMAug 10
to jmeter-...@googlegroups.com
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

// Read response
def response = prev.getResponseDataAsString()

// Parse JSON
def json = new JsonSlurper().parseText(response)

// Remove "P_Response" from every object
json.each { item ->
    item.remove("P_Response")
}

// Convert back to JSON string
def filteredJson = JsonOutput.prettyPrint(JsonOutput.toJson(json))

// Write to file
def filePath = "filtered_response.txt" // Change to your desired path
new File(filePath).text = filteredJson

log.info("Filtered response saved to: " + filePath)

Mohd Sarfraz

unread,
Aug 11, 2025, 5:59:50 AMAug 11
to jmeter-...@googlegroups.com
Hi 
Salim, 

I am observing this error.
2025-08-11 15:27:16,604 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap$Entry.remove() is applicable for argument types: (String) values: [quote_response_xml__c]
Possible solutions: getAt(java.lang.String)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.17.jar:3.0.17]
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.17.jar:3.0.17]
at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:230) ~[ApacheJMeter_core.jar:5.6.2]
at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.6.2]
at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:973) [ApacheJMeter_core.jar:5.6.2]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:585) [ApacheJMeter_core.jar:5.6.2]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) [ApacheJMeter_core.jar:5.6.2]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) [ApacheJMeter_core.jar:5.6.2]
at java.lang.Thread.run(Thread.java:829) [?:?]



Sincerely



Shaik Salim

unread,
Aug 11, 2025, 7:08:44 AMAug 11
to jmeter-...@googlegroups.com
To create a BeanShell script for JMeter that performs the same actions as the Groovy script you provided, you'll need to use Java syntax to handle JSON parsing and file operations. Here's a script that accomplishes this.
BeanShell Script for JMeter
import org.apache.jmeter.protocol.java.sampler.JMeterJavaSampler;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.apache.commons.io.FileUtils;

// Get the response data from the previous sampler
String response = prev.getResponseDataAsString();

// Parse the JSON string
JSONArray jsonArray = new JSONArray(new JSONTokener(response));

// Iterate through the array and remove the "P_Response" key from each object
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    if (jsonObject.has("P_Response")) {
        jsonObject.remove("P_Response");
    }
}

// 

Dmitri T

unread,
Aug 13, 2025, 11:15:37 AMAug 13
to jmeter-plugins
You can try the following code:

def data = new groovy.json.JsonSlurper().parse(prev.getResponseData())


data.Responses = data.Responses.findAll {
it.P_id in ["123", "124"]
}.collect { response ->
response.findAll { key, value -> key != "P_Response" }
}

def jsonBuilder = new groovy.json.JsonBuilder(data)
new File('test.txt').text = jsonBuilder.toPrettyString()


More information:

Reply all
Reply to author
Forward
0 new messages