5

I am trying to get value of TxnType from json request while mocking a response from SOAPUI. I want to respond with different responses based the value of TxnType.

{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}
Rao
  • 20,781
  • 11
  • 57
  • 77

2 Answers2

4

Here is the groovy script to fetch the request value with fixed json

def json = """{
    "Request": {
        "UserId": "user",
        "TxnType": "fetch"
    }
}"""

def transactionType = new groovy.json.JsonSlurper().parseText(json).Request.TxnType
log.info "TxnType is : ${transactionType}"

You may quickly try the Demo

If you want to use the dynamic json in the mock script, then you can use below mock script dispatcher

def transactionType = new groovy.json.JsonSlurper().parseText(mockRequest.requestContent).Request.TxnType
log.info "TxnType is : ${transactionType}"
Rao
  • 20,781
  • 11
  • 57
  • 77
1

// Pick Request Body

def requestBody = mockRequest.getRequestContent()
log.info "Request body: " + requestBody

// Pick TxnType value

def txnType = requestBody["TxnType"]

(or something like this)