Home > Enterprise >  Split the String extracted from json extractor in Jmeter with sets of n
Split the String extracted from json extractor in Jmeter with sets of n

Time:11-02

I have a scenario in which an API responds with a value occurring more than 1000 times and i capture it using Json extractor with ${variable}_all.

I need to split it into sets of 50 or less and parametrise those sets of 50 multiple times into the next API call.

Is there any way to accomplish this ?

CodePudding user response:

Solution 1

You need to slice the values from the JSON Response. You can use JSON JMSEPath Extractor to slice the first fifty. values from the large number of values.

List and Slice Projections

Solution 2 If you want to work with the concatenated result following example could be useful.

def values_ALL = vars.get("<variable name>_ALL")
def filteredValues = values_ALL.split(",")[0..50]

for (i in 0..<filteredValues.size()) {
    println(filteredValues[i])
}

Note : JMeter variable names are case sensitive

CodePudding user response:

  1. Add Loop Controller and use the following __groovy() function as the "Loop Count"

    ${__groovy(vars.get('variable_ALL') as int / 50,)}
    
  2. Inside the Loop Controller you can use JSR223 PreProcessor to generate the request body and refer each "chunk" of the variables like:

    def offset = (vars.get('__jm__Loop Controller__idx')) as int * 50
    
    def payload = new StringBuilder()
    
    offset.upto((offset   50), { index ->
        payload.append(vars.get('variable_'   index)).append(System.getProperty('line.separator'))
    })
    
    sampler.addNonEncodedArgument('', payload.toString(),  '')
    sampler.setPostBodyRaw(true)
    

More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

If you need to produce JSON payload you may find Parsing and producing JSON article interesting as well

  • Related