Search code examples
jmeterpreprocessorjsr223

Jmeter Construct Parameter Value based on DataSet


In JMeter, I want to construct the request parameter value from a dataset file based on the PropertyCount.

Dataset

PropertyCount   propertyid1 propertyid2 propertyid3
2               13029526    15763743        
3               13029526    15763743    12345645    
2               13029526    15763743    

Request Input Parameter

"values":["13029526","15763743"]

"values":[${outputString}]

PreProcessor Script

With the the below preprocessor script, I am getting the following output but looking to get the values as in Request Input Parameter, with quotes.

2021-08-29 22:15:04,706 INFO o.a.j.m.J.JSR223 PreProcessor: Required output: 13029526,15763743, 2021-08-29 22:15:04,785 INFO o.a.j.m.J.JSR223 PreProcessor: Required output: 13029526,15763743,

JSR223 PreProcessor

def requiredOutput = new StringBuilder()
1.upto(vars.get('propertycount') as int, {
     requiredOutput.append(vars.get('propertyid' + it))
     requiredOutput
    requiredOutput.append(',')
    vars.put("outputString",requiredOutput.toString());

})

Solution

  • You're seem to be constructing a JSON Array therefore it makes more sense to consider using Groovy's JsonBuilder instead of doing manual string concatenation:

    def outputString = []
    
    1.upto(vars.get('PropertyCount') as int, {
        outputString.add(vars.get("propertyid$it"))
    })
    
    vars.put('outputString', new groovy.json.JsonBuilder(outputString).toPrettyString())
    

    More information: