Search code examples
performancegroovyjmeterjsr223

Receiving a data array via JSR223 PreProcessor jmeter


Task:

  • there are 2 requests

  • the first request contains data. their number can be from 0 to 20 — in the second request you need to send a real list of this data in the parameters of the second request. There may be 20 lines, or there may be 0

  • in the first query using Regular Expression Extractor I extract all the data into a variable VAR.

  • then in the second request I added JSR223 PreProcessor

And here’s the question: what code should I add to it to solve my problem and pass the values I receive to the body of this request?


Solution

  • We cannot tell you "what code should you add to it to solve your problem" because we don't know how JMeter Variables originating from the Regular Expression Extractor look like and how do you need to transform them for the next request. In other words in order to get a comprehensive answer you need to ask a good question


    To give you an overall idea regarding what steps you need to take:

    If Regular Expression Extractor returns multiple matches like:

    VAR_1=foo
    VAR_2=bar
    VAR_3=baz
    VAR_matchNr=3
    

    you can create an "array" from them in the JSR223 PreProcessor like:

    def array = []
    
    1.upto(vars.get('VAR_matchNr') as int, { index ->
        array.add(vars.get('VAR_' + index))
    })
    
    vars.put('array', new groovy.json.JsonBuilder(array).toPrettyString())
    

    Demo:

    enter image description here

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?