'Is it possible to read all values in the form JSON JMESPath extractor that matches a regular expression in jmeter?

I have following as a response from a GET call and I need to use the values like 100, 101, 102, etc in the payload of another POST call.

Response:

{"id":12,"records":[{"id":100,"documentId":1, "pageNo":1},{"id":101,"documentId":2, "pageNo":1},{"id":102,"documentId":3, "pageNo":1}]}

Required Payload to be used later:

{"id":12,"records":[100,101,102]}

Values like 100, 101, 102 will increase further with increase in data and I would require all of them. I am using this records[*].id as regular expression. If I set match number value to 1, it returns me single value only. But I need all the values in the form of an array. How can I do so? I am using JSON JMESpath Extractor in JMeter.



Solution 1:[1]

I don't think you can achieve it using JSON JMESPath Extractor, for some reason JMeter developers decided not to stick to the JMESPath language specification so the query like records[*].id returns 3 individual JMeter Variables instead of a JSON Array

Here how it should look like:

enter image description here

and here is what JMeter returns:

enter image description here

So I would recommend going for JSR223 PostProcessor and parse the response/create the next request payload in Groovy.

Example code:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

def request = [:]
request.put('id', response.id)
request.put('records', response.records.id.collect())

vars.put('payload', new groovy.json.JsonBuilder(request).toPrettyString())

You should be able to refer the generated request data as ${request} where required.

More information:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T