Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JMeter: Need to send an array of random values from a CSV file

I have a CSV file, where topics of interest are present in each row:

List of entries in a CSV file

I want to pick any 4 random values from the CSV file and pass it as an array of strings to the request body, like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"interest": ["BI Modernization", "Salesforce.com / Net Suite", "Data Monetization", "SDN - Software Defined Networks"]}

How can this be achieved?

>Solution :

You can generate the request body using JSR223 PreProcessor and the following code:

def entries = new HashSet()

def linesFromCSV = new File('/path/to/your/file.csv').readLines()

while (entries.size() < 4) {
    entries.add(linesFromCSV.get(org.apache.commons.lang3.RandomUtils.nextInt(0, linesFromCSV.size())))
}

def payload = new groovy.json.JsonBuilder([interest: entries]).toPrettyString()

vars.put('payload', payload)

Generated request body can be referenced as ${payload} where required

More information:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading