简体   繁体   中英

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
  ]
}
  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创建JSON

References:

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} ] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM