Search code examples
arraysapijmeterresponsejmespath

Jmeter JMESPath Extract and converting to an Array


Hi is there's a way I can simulate this scenario using JSONPath? Example I have multiple City value and I want all to extract and put it on an Array. Your response is highly appreciated. Thank you so much.

[ { "id": "MAIN", "key": 1 }, { "city": "Roselle", "id": "87", "state": "IL", "key": 1000 }, { "city": "Chicago", "id": "7102", "state": "IL", "key": 110 }, { "city": "Crown Point", "id": "7106", "state": "IN", "key": 110038 } ]

Screenshot:

enter image description here

Expected Output:

enter image description here


Solution

    1. Add JSR223 PostProcessor as a child of the request which returns the above JSON

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

      vars.put('cities', new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parse(prev.getResponseData()).findResults{entry -> entry.city}).toPrettyString())
      
    3. Refer generated value as ${cities} where required

    Demo:

    enter image description here

    More information: