Search code examples
groovyjmeter

Use Groovy to change inline Jmeter variable JSON obj in Body Data


Idea is to get Jmeter variable notification which is JSON obj, edit notification.isRead prop and parse it back to string for posting. But something is wrong with assign operator. With = I get nothing, without returns parsed JSON (as expected).

With this I am almost there, but I can't figure out how to edit prop isRead

$ {__groovy
    (
        (
            new groovy.json.JsonBuilder(

                new groovy.json.JsonSlurper().parseText(vars.get('notification')).isRead = false

            ).toPrettyString()
        )
    )
}

Obj:

notification: {
    "isRead": false, 
}

Solution

  • So apparently, this would be the correct syntax.

    ${__groovy
    (
        def json = new groovy.json.JsonSlurper().parseText(vars.get('notification')) 
        json.isRead = false
    
        ( new groovy.json.JsonBuilder(json).toPrettyString())
        )}