Home > Mobile >  JMeter- How to pass individual values from a string of values using JSON extractor
JMeter- How to pass individual values from a string of values using JSON extractor

Time:07-06

In JMeter, I have extracted a string of values from a response using JSON extractor (I'm storing all the occurrences of the variable by checking 'Compute Concatenation var' checkbox). The values are stored in a variable called identifier_ALL. It looks like this:

identifier_ALL = 123, 456, 789

In the above example, I've got 3 values, but it'll differ for each request. I need to pass the individual values to the body of a request, so I used the below syntax:

{"content_id": ["${identifier_ALL}"]}

which will pass the values like this: {"content_id": ["123, 456, 789"]}

But I want the ids to be passed like below:

Expected: {"content_id": ["123", "456", "789"]}

I don't want to write these values to a CSV file and fetch the value from there again, as I'm running my script for 5000 users, and this will only add up to memory utilization and delay in response. Is there any other way I can use to pass the values in the expected format?

CodePudding user response:

Unfortunately this is not something you can achieve using JSON Extractor, you will need to:

  1. Add a JSR223 PostProcessor after the JSON Extractor

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

    def rv = new StringBuilder()
    
    1.upto(vars.get('identifier_matchNr') as int, {
        rv.append('"').append(vars.get('identifier_'   it)).append('"')
        if (it as String != vars.get('identifier_matchNr')) {
            rv.append(',')
        }
    })
    
    vars.put("identifier_INDIVIDUAL", rv.toString())
    
  3. In your next request use {"content_id": [${identifier_INDIVIDUAL}]}

In the above example vars stands for JMeterVariables class instance, check out the JavaDoc for all available methods and fields and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details on this and other JMeter API shortcuts available for the JSR223 Test Elements.

  • Related