-1

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.

Sandesh
  • 9
  • 2

1 Answers1

0

This line:

Encountered "}" at line 44, column 25.

means that you have extra } which doesn't match the opening one so I don't think the code is the "same"

Also you will need to call the main function explicitly, Beanshell doesn't have an entry point and processes the code upside down.

Another thing is that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting

and finally if you run your code with 2 or more threads you will face a race condition resulting in data corruption and/or loss so I would rather suggest extracting the values from JSON using JSON Extractor and writing them into the CSV file using Flexible File Writer

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • thank you solved my problem. The problem was that it did not have to have main method and class. – Sandesh Jan 02 '23 at 11:17