jsongroovyjmeterbeanshellpost-processor

JMeter: Update Empty JSON hashmap groovy


Response from http request:

{"Preferredvalue":{"notations":[]}}

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

I am able to get up to notations and also the size.

If the size is 0, I want to update the notations as below

{"Preferredvalue":{"notations":[{"Name":${firstName},"lName":${lastName}}]}

firstName and lastName are Jmeter variable which are fetched from another calls, and I want to use these values in my another call and send a PUT request.

Searched a lot but couldnt find an answer :(

Best,


Solution

  • Something like:

    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    def notations = response.Preferredvalue.notations
    
    if (notations.size() == 0) {
        notations.add([Name: vars.get('firstName'), lName: vars.get('lastName')])
    }
    
    def request = new groovy.json.JsonBuilder(response).toPrettyString()
    
    vars.put('request', request)
    

    should do the trick for you. Refer generated value as ${request} where required

    More information: