0

https://stackoverflow.com/a/27637247/12820240

I saw above answer (https://stackoverflow.com/a/27637247/12820240) from another question, but my questions is, Is there anyway that that I can build large Json Request by adding comma separated with new data? Would appreciate for any help/direction. for ex:

{
{
  "phoneNo": "9998885551",
  "lastName": "john25",
  "email": "azp25@gmail.com",
  "firstName": "ricky25",
  "mobileNo": "9820420420"
},
{
  "phoneNo": "9998885552",
  "lastName": "john26",
  "email": "azp26@gmail.com",
  "firstName": "ricky25",
  "mobileNo": "9820420421"
},
{
  "phoneNo": "9998885553",
  "lastName": "john27",
  "email": "azp27@gmail.com",
  "firstName": "ricky27",
  "mobileNo": "9820420422"
}

...

}

Thank you

1 Answers1

0

Given your CSV file looks like:

9998885551,john25,azp25@gmail.com,ricky25,9820420420
9998885552,john26,azp26@gmail.com,ricky26,9820420421
  1. Add JSR223 PreProcessor as a child of the HTTP Request sampler which body you want to generate
  2. Put the following code into "Script" area:

    def body = []
    new File('/path/to/your/file.csv').readLines().each { line ->
        def entry = [:]
        def values = line.split(',')
        entry.put('phoneNo', values[0])
        entry.put('lastName', values[1])
        entry.put('email', values[2])
        entry.put('firstName', values[3])
        entry.put('mobileNo', values[4])
        body.add(entry)
    }
    sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(body).toPrettyString(),'')
    sampler.setPostBodyRaw(true)
    
  3. That's it, the JSR223 PreProcessor will generate the request body out of the CSV file

    enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133