MBB
MBB

Reputation: 3

How to have the data in the CSV file converted to JSON in the request body when using JMETER CSV Date set config

I have a CSV dataset config pointing to a CSV file with the following data:

Ids
87541
4551
15441
11117
.....
n

Instead of looping through the file and do multiple POST requests for each value, I need to have a single POST request and pass ALL the IDs the request body which should like this in the generated JSON:

{
  "ids": [
    84280,
    2334,
    235,
    32554,
    3663,
    346,
    344643,
    ....,
    n
  ]
}

Upvotes: 0

Views: 1073

Answers (2)

Ori Marko
Ori Marko

Reputation: 58872

In CSV Data Set Config define delimiter which not in file as ~

Put in variable name ids for example.

and then use it in request as { "ids": [ ${ids} ] }

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168157

  1. Add JSR223 PreProcessor as a child of the request which needs to send this JSON.
  2. Put the following code into "Script" area

    def csvfile = new File('test.csv')
    def jsonBuilder = new groovy.json.JsonBuilder()
    
    jsonBuilder {
        ids csvfile.collect { it }
    }
    
    vars.put('requestBody', jsonBuilder.toPrettyString())
    log.info(vars.get('requestBody'))   
    

    The above code will read test.csv file in JMeter's "bin" folder and create an ids JSON Array where each element will be a line from the given file and put the result into ${requestBody} JMeter Variable

Demo:

JMeter Groovy Create JSON

References:

Upvotes: 1

Related Questions