Search code examples
groovyjmeter

How to merge desired no. of json blocks into one based on no. of days difference (exclusive end date in calculation) in jmeter


I want to generate multiple json blocks and to merge them in one to pass in a request. Below is the sample block,

    {
        "roomId": "1429293",
        "daysCount": "9173",
        "a": "F",
        "x": "0",
        "t": "0",
        "y": "-",
        "l": "0"
    }

But there are some conditions for each block,

  1. want to generate it for next 300 days (so there would be 300 json blocks to be merged) each having same roomId.
  2. daysCount for each block will be different. For first block, It is the days difference between two dates i.e 01-Jan-2000 and 11-Feb-2025 (9173), for second block It is the days difference between i.e 01-Jan-2000 and 12-Feb-2025 (9174), for third block It is the days difference between i.e 01-Jan-2000 and 13-Feb-2025 (9175) and so on..
  3. roomId will be passed from the previous response. roomIds can be more than 1, in that case no. of blocks will be 600 (i.e 300 for each roomId).
  4. rest of the values will be same for all blocks.

I have tried doing it with JSR post processor with groovy code but no success. Can someone please help with the code. Thanks in advance.


Solution

  • As far as I got your requirement it would be something like this:

    def payload = []
    
    1.upto(vars.get('foo_matchNr') as int, { roomId ->
        def startValue = 9173
        1.upto(300, block -> {
            def entry = [roomId: vars.get('foo_' + roomId), daysCount: startValue as String, a: "F", x: "0", t: "0", y: "-", l: "0"]
            startValue = startValue + 1
            payload.add(entry)
        })
    
    })
    
    vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
    

    More information: