How to set json object value as request in soapUI

I have a json request:

{ "scopeId":"", "scopeType":"", "userId":"", "fieldToBeEncryptedList":[ { "srNo":"", "fieldName":"", "value":"", "echoField":"" } ] } 

Now I want to set the dynamic value of each key that I received from the answer of another test step. How can I do this using groovy scripting.

Thanks at Advance.

+5
source share
2 answers

There are some details missing from your question, but I suppose you have two REST TestSteps , assuming the first is called REST Test Request , and the second REST Test Request2 you can set the answer and get the request values ​​for your test steps using the groovy script inside a groovy TestStep :

 import groovy.json.JsonSlurper import groovy.json.JsonOutput // request def request = '''{"scopeId":"", "scopeType":"", "userId":"", "fieldToBeEncryptedList":[{"srNo":"","fieldName":"","value":"","echoField":""}] }''' // parse the request def jsonReq = new JsonSlurper().parseText(request); // get the response where you've the values you want to get // using the name of your test step def response = context.expand('${REST Test Request#Response}') // parse response def jsonResp = new JsonSlurper().parseText(response) // get the values from your first test response // and set it in the request of the second test jsonReq.scopeId = jsonResp.someField jsonReq.scopeType = jsonResp.someObject.someField // ... // parse json to string in order to save it as a property def jsonReqAsString = JsonOutput.toJson(jsonReq) // save as request for the next testStep def restRequest = testRunner.testCase.getTestStepByName('REST Test Request2'); restRequest.setPropertyValue('Request',jsonReqAsString); 

These scripts get your json and populate it with values ​​from the first testStep response, and then set that json as a request for the second testStep .

Hope this helps,

+5
source

You may have to do something like this:

 ​import groovy.json.JsonBuilder import groovy.json.JsonSlurper def jsonStr = '{"scopeId":" ","scopeType":" ","userId":" ","fieldToBeEncryptedList":[{"srNo":" ","fieldName":" ","value":" ","echoField":" "}]}' def slurp = new JsonSlurper().parseText(jsonStr) def builder = new JsonBuilder(slurp) builder.content.scopeId = 'val1' builder.content.fieldToBeEncryptedList[0].srNo = 'val2' println builder.toPrettyString()​ 

Conclusion:

 { "fieldToBeEncryptedList": [ { "echoField": " ", "fieldName": " ", "srNo": "val2", "value": " " } ], "scopeId": "val1", "scopeType": " ", "userId": " " } 
0
source

All Articles