How to write extracted result in csv file using beanshell jmeter?

4.9k Views Asked by At

I am getting HTTP response in JSON format. Using JSON extractor I am able to extract exact value but I am not able to store them in CSV file.

JSON response

{
    "success": true,
    "vulns":     [
                {
            "TYPE": "vuln",
            "ID": "82003",
            "FQDN": "",
            "PORT": "",
            "Table": "67696056"
        },
                {
            "TYPE": "vuln",
            "ID": "90067",
            "FQDN": "",
            "PORT": "",
            "Table": "67696057"
        },
                {
            "TYPE": "vuln",
            "ID": "70000",
            "FQDN": "",
            "PORT": "",
            "Table": "67696058"
        },
                {
            "TYPE": "vuln",
            "ID": "70032",
            "FQDN": "",
            "PORT": "",
            "Table": "67696059"
        },
                {
            "TYPE": "vuln",
            "ID": "90042",
            "FQDN": "",
            "PORT": "",
            "Table": "67696060"
        }
    ]
}

Here I want All the ID to be stored in .csv file to use them in future. I used following code in beanshell postprocessor

import java.io.FileWriter;

String str=vars.get("ID");

FileWriter writer = new FileWriter("C:\\Softwares\\Installed\\jmeter-3.0\\bin\\ID.csv"); 

writer.write(str);

writer.close();

It is writting the file but all the ID's are written in one line like

["82003","90067","70000","70032","90042"]

but I need them like

82003
90067
70000
70032
90042

Debug postprocessor response data

ID=["82003","90067","70000","70032","90042"]

can some please provide me solution or BeanShell code for the same?

thanks, Vijay

3

There are 3 best solutions below

0
On BEST ANSWER

I believe you can kill two birds with one stone using JSR223 PostProcessor and Groovy language instead of JSON Extractor and Beanshell.

  1. Add JSR223 PostProcessor as a child of the request which produces that JSON output
  2. Put the following code into PostProcessor's "Script" area:

    import groovy.json.JsonSlurper
    import org.apache.commons.io.FileUtils
    
    def response = prev.getResponseDataAsString()
    
    JsonSlurper slurper = new JsonSlurper()
    def json = slurper.parseText(response)
    
    def ID = json.vulns.ID
    
    ID.each { FileUtils.writeStringToFile(new File("C:\\Softwares\\Installed\\jmeter-3.0\\bin\\ID.csv"), it + System.getProperty("line.separator"), true) }
    

You should get the file generated in the format you want.

References:

0
On

If you are extracting the exact value using a JSON Extractor, you can use the following code to write variables in new line.

    try {
      extractedId=vars.get("extracted_Id");
      f= new FileOutputStream("D:/YourCSVFile.csv", **true**);
      p = new PrintStream(f); 
      this.interpreter.setOut(p);
      print(extractedId);
      f.close();
   }

  catch (Throwable ex) {
     log.error("Error in Beanshell", ex);
     throw ex;
  }

In the FileOutputStream function use 'false', if you want to create new fresh results in every setup. Putting 'true' will append into the same file in your every run of that Test plan

0
On

try this, it worked for me

ID=vars.get("ID");

FileWriter writer = new FileWriter("C:\\Softwares\\Installed\\jmeter-3.0\\bin\\ID.csv",true); // true to append 

BufferedWriter out = new BufferedWriter(writer);
 out.write(ID);
 out.write(System.getProperty("line.separator"));
 out.close();

writer.close();