arraysjmeterresponsejmeter-5.0

Extracting JSON response and putting them inside a customize JSON


Hi can anyone help me how to simulate this scenario. Thank you so much in advance and your response is highly appreciated.

Example I have this response below

[{
        "agentid": "9f235146-394d-3200-bff4-ff820c7cdb8e",
        "os": "",
        "fqdn": "prf_loadtest-54"
    },
    {
        "agentid": "33777a51-8696-3c53-97ab-5944224ae5e4",
        "os": "",
        "fqdn": "prf_loadtest-389"
    },

    {
        "agentid": "b6e859d0-2fb4-307d-b3b6-a6daa0b58fc2",
        "os": "",
        "fqdn": "server-13-35-77-127.bos50.r.cloudfront.net"
    }
]

and I want to extract all fqdn prf_loadtest agentId and use it on this request below, take note the agentId is DYNAMIC so example if I extracted 1 or 2 and etc it will use sample request below

if 1 extracted agent id

[{
    "key": "9f235146-394d-3200-bff4-ff820c7cdb8e",
    "value": "AD SERVER"
}]

if 2 extracted agentid

[{
    "key": "9f235146-394d-3200-bff4-ff820c7cdb8e",
    "value": "AD SERVER"
}, {
    "key": "33777a51-8696-3c53-97ab-5944224ae5e4",
    "value": "AD SERVER"
}]

Solution

  • You could use JSR223 PostProcessor and the following code:

    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    def request = response.findAll { entry -> entry.fqdn.contains('loadtest') }.collect { id -> [key: id.agentid, value: 'AD SERVER'] }
    
    vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())
    

    the generated request can be accessed as ${request} where required.

    Demo:

    enter image description here

    More information: