user1420688
user1420688

Reputation: 67

xpath and soapui, Transfer Property. Getting nested JSON from a Get to a Post in a Test Suite

I am trying to learn SoapUI and I have not found any good guides on how to perform a transfer property from a Rest GET request to a Rest POST request.

I have the following payload returned from a REST GET request

{
 "a":"a",
 "b": { "b1":"b1", "b2":"b2" },
 "c":"c"
}

I want to take this JSON and remove c, then submit the rest to a POST request. I wish to post

{
 "a":"a",
 "b": { "b1":"b1", "b2":"b2" },
}

I am trying to do all this in SoapUI, but have had no success. I am able to get individual values by saying in the source property is RseponseAsXML and the target property is Request. Then I use the command //*:a. But it only returns that value.

I do not want this to be xml though, I am working strictly with JSON. Thank you.

Upvotes: 1

Views: 766

Answers (1)

albciff
albciff

Reputation: 18507

If you want to manipulate the JSON response to use in other TestStep it's easier to use groovy TestStep instead of Transfer testStep. So create a groovy TestStep and manipulate the response using the following code:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

// get the response using the name of your test step
def response = context.expand('${REST Test Request#Response}')
// parse response
def jsonResp = new JsonSlurper().parseText(response)
// remove "c" element
jsonResp.remove("c")
// parse json to string in order to save it as a property
def jsonAsString = JsonOutput.toJson(jsonResp)
log.info jsonAsString // prints --> {"b":{"b1":"b1","b2":"b2"},"a":"a"}
// save the json response without "c" in a testCase property
testRunner.testCase.setPropertyValue('json_post',jsonAsString)

With the code above you parse the response, remove the c element and save as a property in the testCase then in your REST POST you can use the follow code to get your new JSON:

${#TestCase#json_post}

Hope this helps,

Upvotes: 1

Related Questions