0

I have two variables whose values i have extracted from the response body using JSON Extractor [Here is the image showing two variables][1] [1]: https://i.stack.imgur.com/E0RKx.png

In JSR223 PostProcessor, I have written below code which prints all the values in 1st column, but I want 2 separate columns variable wise.

import org.apache.commons.io.FileUtils
import java.text.SimpleDateFormat;
// Get total number of matches. (Returns string)
def resultCount = vars.get("AppObjectName_matchNr")
log.warn 'Output to jmeter console' +  resultCount
// Generate timestamp to create uniue file name.
String fileSuffix = new SimpleDateFormat("ddMMyyyyHHmm").format(new Date())
// Create file Object
f = new File("results_Kr"+fileSuffix+".csv")
for (int i=1; i<=resultCount.toInteger(); i++)
{
  // Get each matching result one by one.
  records = vars.get("AppObjectName_" +i)
  Query = vars.get("QueryName_" + i)
// Write result to csv file.
  FileUtils.writeStringToFile(f,records + System.getProperty("line.separator"),true)
  FileUtils.writeStringToFile(f,Query + System.getProperty("line.separator"),true)
}

[Here is the image of code which creates .csv file & write both variables values in one column only][2] [2]: https://i.stack.imgur.com/bVIeb.png

1 Answers1

0
  1. Change first FileUtils.writeStringToFile function call to:

    FileUtils.writeStringToFile(f,records + ',' + Query + System.getProperty("line.separator"),true)
    
  2. Remove second function call completely

Also be aware that in case of minimal concurrency when 2 or more threads will be writing data into the same file it might result into data corruption or loss so if this is the case - consider using either Flexible File Writer or convert the variables into JMeter Properties and write them in tearDown Thread Group.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • There is any command to overwrite existing data in csv file ? – Krina Modi Apr 12 '23 at 10:07
  • You can either delete the file somewhere in [setUp Thread Group](https://www.blazemeter.com/blog/thread-group-jmeter) or during first iteration of your for loop write the first line to the file without appending to existing by passing `false` to FileUtils.writeStringToFile()` function – Dmitri T Apr 12 '23 at 10:27