Search code examples
jmeterbeanshell

Process of Array of Strings in Beanshell preprocessor in Jmeter


I have a Jmeter (version 4.0) test script where I am using json extractor to fetch an array of strings from the response json using $..Names and storing it in a variable groupOfNames. The various names are stored like groupOfNames_1, groupOfNames_2, groupOfNames_ALL.

I need to make POST call next with body as

{
"name1", "name2", "name3" (--actual values--)
}

How can i achieve this using bean shell preprocessor? groupOfNames_ALL gives me all value but like this.... name1, name2, name3 (without quotes surrounding individual names). Please help. Thanks.


Solution

  • I heard Groovy is the New Black so you can add quotation marks around each of names as simply as:

    vars.put('groupOfNames_ALL',vars.get('groupOfNames_ALL').split(',').collect {"\"$it\""  }.join(', '))
    

    Demo:

    JMeter Groovy Add Quotation Marks Around Strings

    Also as a gentle reminder: JMeter users are encouraged to use JSR223 Test Elements for any form of scripting since JMeter 3.1