Search code examples
jmeterresponsejsonpathjson-path-expressionjson-extract

Extracting Json response using Json extractor on Jmeter


Is there's a way I could extract the highest id: "15" of bld-appliance-1 using Json Path expression. Currently if use this Json expression "[?(@.name == 'bld-appliance-1')]..id" it returns me 3 result. I'm also aware I could use Match No but my catch here is the response is DYNAMIC I don't know if the 1st, 2nd or 3rd and etc is the highest id upon invocation. Your response is highly appreciated. Thank you so much.

Sample Response:

[{

    "id": "5",
    "name": "bld-appliance-1",
    "hostName": "bld-appliance-1"
},

{
    "id": "10",
    "name": "bld-appliance-1",
    "hostName": "bld-appliance-1"
},

{
    "id": "15",
    "name": "bld-appliance-1",
    "hostName": "bld-appliance-1"
},
{
    "id": "20",
    "name": "test-appliance-1",
    "hostName": "test-appliance-1"
}, {
    "id": "25",
    "name": "uat-appliance-1",
    "hostName": "uat-appliance-1"
}

]

Screenshot


Solution

  • You can do it using JSR223 PostProcessor and the following Groovy code:

    def id = new groovy.json.JsonSlurper().parse(prev.getResponseData()).findAll {entry -> entry.name == 'bld-appliance-1'}.collect{entry -> entry.id as int}.max()
    

    in case you need to store the result into a JMeter Variable you can add the next line:

    vars.putObject('id', id)
    

    More information: