Shavaiz Safdar
Shavaiz Safdar

Reputation: 31

How to extract an array using json extractor and parse it to remove dupes in Jmeter

JSON response is returned in the following format, I need to extract values from the first index of the array for example 925,88 using groovy or perhaps another language, and then store them into a variable to pass them along in the next request. Also, values need to be unique

[
    [
        22588,
        [
            925,
            88
        ],
        0,
        0,
        0,
        null,
        "moderate"
   ]

Any help would be appreciated!

Thanks

Upvotes: 0

Views: 1198

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

  1. Add JSR223 PostProcessor after the JSON Extractor

  2. Put the following code into "Script" area:

    def numbers = []
    
    1.upto(vars.get('foo_matchNr') as int, { index ->
        new groovy.json.JsonSlurper().parseText(vars.get('foo_' + index)).each { number ->
            numbers.add(number)
        }
    })
    
    vars.put('payload', new groovy.json.JsonBuilder(numbers.sort().unique()).toPrettyString())
    
  3. Replace foo with the actual JMeter Variable name you use in the JSON Extractor

  4. Use ${payload} where you need to send unique numbers

More information:

Upvotes: 1

Related Questions