I'm inviting you to a new Community challenge which we are starting next Monday (April 2)! Earn badges to get more points. Read more information
Reply
Highlighted
Frequent Contributor

Capture an Entire JSON response to a property?

Hi,

 

I feel a bit dumb asking this question cos I feel like I should know the answer.

 

Anyway - I have a POST request that generates a .json response.

 

I would like to capture the whole request's response into a single property.

 

Does anyone know how to do this?

 

example of the .json is as follows:

 

{
"@odata.context": "https://XXXX.dynamics.com/api/data/v8.2/$metadata#phonecalls",
"value": [
{
"@odata.etag": "W/\"3844463\"",
"_owningbusinessunit_value": "a6ae8ca6-7ae3-e711-8118-5065f38bc491",
"statuscode": 127130002,
"activityid": "2266985a-013f-e811-8122-5065f38be511",
"actualdurationminutes": 30,
"activityadditionalparams": null,
}, {
"@odata.etag": "W/\"3844464\"",
"_owningbusinessunit_value": "a6ae8ca6-7ae3-e711-8118-5065f38bc491",
"statuscode": 127130002,
"activityid": "ee104a67-013f-e811-8122-5065f38be511",
"actualdurationminutes": 30,
"activityadditionalparams": null,
}, {
"@odata.etag": "W/\"3844465\"",
"_owningbusinessunit_value": "a6ae8ca6-7ae3-e711-8118-5065f38bc491",
"statuscode": 127130002,
"activityid": "466c0c74-013f-e811-8122-5065f38be511",
"actualdurationminutes": 30,
"activityadditionalparams": null,
}
]
}

I think I could do this in XML because you just take the root tag in the transfer I'm guessing - but I can't seem to do that when the response is .json.  I was playing around using an XMLDatasource with the source property option of 'ResponseAsXML', but I was unsuccessful and am unsure if this will achieve what I want.

 

Again - thanks to all for any helpful hints/tips/guidance,

 

richie

Contributor

Re: Capture an Entire JSON response to a property?

[ Edited ]

Hi Richie,

 

Well you don't have to save the whole json response in a property because ReadyApi already does that automatically for you. Smiley Very Happy

 

For instance, I have a request which returns the following raw response:

 

HTTP/1.1 200 OK
Content-Type: application/json
Server: Jetty(6.1.26)

{ "key" : "value" }

When the request is finished the JSON is automatically saved as:

 

Sketch.png

 

You can later reference this by using the usual SoapUI syntax: ${NameOfYourTestCase#Response}

Contributor

Re: Capture an Entire JSON response to a property?

You can get an entire JSON response in an assertion script step.

 

import groovy.json.JsonSlurper
def resp = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(resp)

 

from there you can access fields like jsonSlurper.fieldName1

 

or output the json 

log.info jsonSlurper.toString()

 

I know you really want to do this in an ordinary Groovy script though.

 

I have a real working test case structured with the following steps

Test case xyz

   groovy script 1

  properties (2)

  request step 1

  groovy script 2

  request step 2

 

So in my groovy script 2 I have code like

 

import static groovy.json.JsonOutput.toJson;
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

def previousTestStep = testRunner.getTestCase().getTestStepByName("request step 1")
String propertySpec = '${' + previousTestStep.name + '#Response}'
def response = context.expand(propertySpec)
def jsonSlurper = new JsonSlurper().parseText(response)



I probably don't use the first import in the code above. 

 

I use JsonBuilder to construct new json and then set the json for the request step 2.

 

You can print out the json from the response (I assume response to your POST)

 

log.info jsonSlurper.toString()

 

The catch is that you nave to know the name of the request test step.

 

Bill