So, I am using BeanShell PostProcessor to write the json data to csv file . This is my code in beanshell postprocessor.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.opencsv.CSVWriter;
public class CsvWrite{
public static void main(String[] args){
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
File file = new File("D:\\verisk\\first_url_response.csv");
try {
FileWriter fileWriter = new FileWriter(file);
CSVWriter csvWriter = new CSVWriter(fileWriter);
// adding header to csv
String[] header = { "id", "title", "description", "price", "discountPercentage", "rating", "stock",
"brand" };
csvWriter.writeNext(header);
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
JSONArray productArray = (JSONArray) jsonObject.get("products");
int productArraySize = productArray.size();
for (int i = 0; i < productArraySize; i++) {
JSONObject productDetailObject = (JSONObject) productArray.get(i);
String id = (productDetailObject.get("id").toString());
String title = (productDetailObject.get("title").toString());
String price = (productDetailObject.get("price").toString());
String description = (productDetailObject.get("description").toString());
String discountPercentage = (productDetailObject.get("discountPercentage").toString());
String rating = (productDetailObject.get("rating").toString());
String stock = (productDetailObject.get("stock").toString());
String brand = (productDetailObject.get("brand").toString());
String[] productDetails = { id, title, description, price, discountPercentage, rating, stock, brand };
csvWriter.writeNext(productDetails)
}
fileWriter.close();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
However, it does not write to csv. on using the log command following error is shown.
2023-01-02 14:41:06,853 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``import java.io.File; import java.io.FileNotFoundException; import java.io.FileWr . . . '' Encountered "}" at line 44, column 25.
2023-01-02 14:41:06,853 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1 2023-01-02 14:41:06,853 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1 2023-01-02 14:41:06,854 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2023-01-02 14:41:06,854 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, local)
Note: I have added json-simple-1.1.jar and opencsv-4.1.jar in lib folder. The code is working with java in eclipse.