Search code examples
jmeterhttp-postperformance-testing

JMeter: Passing a series of string values to an array


I have an array, which I obtained after combining responses from different requests. It looks like this:

testArray = ["123,456,789","1234,5678,9801"]

The first string is from request1, second from request 2 and so on.

But I want to send the array in the below format:

testArray = ["123","456","789","1234","5678","9801"]

Each value should be passed as a string to the body of a request. I converted each string in the required format, but when adding them to the array, it adds backslashes ("") at the beginning and end of each value. Is there any way to send the array to the request body as mentioned above?


Solution

  • If you have testArray JMeter Variable with the value of ["123,456,789","1234,5678,9801"] which looks in Debug Sampler like:

    enter image description here

    and want to transform it to ["123","456","789","1234","5678","9801"] you can do it using a suitable JSR223 Test Element and the following Groovy code:

    def original = new groovy.json.JsonSlurper().parseText(vars.get('testArray'))
    
    def expected = []
    
    original.each { part ->
        part.split(',').each { value ->
            expected.add(value)
        }
    }
    
    vars.put('testArray', new groovy.json.JsonBuilder(expected).toString())
    

    Demo:

    enter image description here