jmetertaurus

Jmeter extracting values from response and sending to other requests


I have a JSON response below:

"books": [
{
   "name" : "test1",
   "id" : "T01"
},
{
   "name" : "test2",
   "id" : "T02"
},
{
   "name" : "test3",
   "id" : "T03"
},
]

I am extracting all respective ids and sending it as a body to another request. Other request takes it as an array of strings but when I am extracting it, it is showing as integers:

Currently it shows:  ids_ALL = [T01, T02, T03]
and I have to pass it like: ids_ALL = ["T01", "T02", "T03"]

Note: I am suffixing _ALL to get all ids.

Since it is not passing the array as string, I am getting an error.

Is there away to extract it and put it in array of strings or way to use post-processer and then convert the array and send to other request.


Solution

  • This one-liner will extract all the IDs and generate the JSON Array you're looking for:

    vars.put('payload', (new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parse(prev.getResponseData()).books.id.collect()).toPrettyString()))
    

    no other extractors are needed, you can refer the generated array as ${payload} later on where required

    In Taurus it can be put into the JSR223 Block

    More information: