Search code examples
arraysjsonregexjmeterrabbitmq

Jmeter: unable to extract value from nested array


I am trying to automate load testing on Jmeter in order to be able to schedule them and let them run at specific times. I have no powers over the queues nor our rabbit scenery in general, I can only send and retrieve.

I can schedule the sending of messages to our own queue on rabbitmq just fine. However, then I need to be able to check if they all made it safely there. So, I extracted all the messageId values from the sent messages, and need to see if the queues contains them.

I managed to get the last (number I need) of messages from the queue as a whole text, it looks like this:

[{
        "payload_bytes": 702,
        "payload": "{\"receiver\":\"randomnumbers\",\"expiration_ts\":\"2024-07-03T15:13:45.812+02:00\",\"la_id\":\"miao\",\"service_name\":\"miao2\",\"la_message_code\":\"05c77901-fb4b-4b9a-8dfc-be2af1d0e2f0\",\"wa_account\":\"393511491882\",\"metadata\":{\"mkt\/servizio\":\"link:it\",\"ubm\/acceptingDate\":\"2024-07-02T15:13:45.772+02:00\",\"ubm\/ubmMessageSid\":\"05c77901-fb4b-4b9a-8dfc-be2af1d0e2f0\",\"miao5\":\"miao3\",\"la\/category\":\"Service\",\"la\/subCli\":\"miao6\",\"mkt\/ambiente\":\"Test\",\"mkt\/groupServiceSid\":\"miao7\",\"afc\/ambiente\":\"Test\",\"mkt\/service_name\":\"miao2\"},\"template_foreign_id\":\"miao4\",\"template_params\":[\"1\",\"text2\",\"text3\",\"text4\",\"text5\",\"text6\",\"text7\",\"text8\",\"text9\",\"text10\"]}",
        "payload_encoding": "string",
        "exchange": "",
        "routing_key": "miaoqueue",
        "message_count": 522,
        "redelivered": true,
        "properties": {
            "headers": {
                "x-delay": 0
            },
            "content_type": "application\/octet-stream",
            "priority": 0,
            "delivery_mode": 2
        }
    },
    {
        "payload_bytes": 702,
        "payload": "{\"receiver\":\"randomnumbers\",\"expiration_ts\":\"2024-07-03T15:13:45.837+02:00\",\"la_id\":\"miao\",\"service_name\":\"miao2\",\"la_message_code\":\"bdc77f92-66d8-45c7-8114-7cde780421d6\",\"wa_account\":\"393511491882\",\"metadata\":{\"mkt\/servizio\":\"miao4\",\"ubm\/acceptingDate\":\"2024-07-02T15:13:45.767+02:00\",\"ubm\/ubmMessageSid\":\"bdc77f92-66d8-45c7-8114-7cde780421d6\",\"miao5\":\"miao3\",\"la\/category\":\"Service\",\"la\/subCli\":\"miao6\",\"mkt\/ambiente\":\"Test\",\"mkt\/groupServiceSid\":\"miao7\",\"afc\/ambiente\":\"Test\",\"mkt\/service_name\":\"miao2\"},\"template_foreign_id\":\"miao4\",\"template_params\":[\"1\",\"text2\",\"text3\",\"text4\",\"text5\",\"text6\",\"text7\",\"text8\",\"text9\",\"text10\"]}",
        "payload_encoding": "string",
        "exchange": "",
        "routing_key": "miaoqueue",
        "message_count": 521,
        "redelivered": true,
        "properties": {
            "headers": {
                "x-delay": 0
            },
            "content_type": "application\/octet-stream",
            "priority": 0,
            "delivery_mode": 2
        }
    }]

However, it runs a lot of times. I also managed to slice it into single payload items via a json extractor:

$..payload

Then I get all of them listed out and numbered using a JSR223:


int payloadCount=vars.get("payload_matchNr").toInteger()
def lstPayload =[]

for(i in 1..payloadCount){
    lstPayload.add(vars.get("payload_" + i))
}
vars.putObject("payload", lstPayload)

My issue is that I can't get all the "la_message_code" entries out of this thing (that's how the messageId value is called here). Once I get them all out, I want to be able to do something like: for each la_message_code, try to see if it contains any of the messageId that I extracted previously.


Solution

  • Groovy has built-in JSON support hence you can do everything in one shot:

    def payloads = new groovy.json.JsonSlurper().parse(prev.getResponseData()).payload
    
    payloads.eachWithIndex { payload, index ->
        vars.put('la_message_code_' + (index + 1), (new groovy.json.JsonSlurper().parseText(payload).la_message_code))
    }
    

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?