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
I believe you can kill two birds with one stone using JSR223 PostProcessor and Groovy language instead of JSON Extractor and Beanshell.
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: