Passing a json node (rather than the node's value) to a Property
Hi,
I need to grab a node name rather than the value from a .json request and pass it onto a subsequent .json request's body.
I have 2 api's I need to test. 1 is a POST followed by a GET. The POST's response body has the same structure as the GETs request body.
My POST's api generates the following .json response
{
"$id": "1",
"UniqueAlphaNumericGUID": {
"$id": "2",
"StatusCode": 0,
"Outcome": "NewCall"
}
}
As you can see the 'UniqueAlphaNumericGUID' is the label (or json node) AND the value.
SoapUI obviously treats this as the node rather than the value - but it is actually the unique reference of a record <-- so the node name changes for each record
I need to do a property transfer of this value to pass onto the GET's request body but I'm unsure how to do this because I need to pass the node name rather than the node's value.
groovyguygave me some groovyscript to parse XML and I tried converting it to parse .json but I'm not going to embaress myself by giving you what I got - its rubbish and doesn't even come close to working.
a java developer gave me the following to start me off:
import groovy.json.JsonSlurper def json = "{\"\$id\": \"1\",\"UniqueAlphaNumericGUID\": {\"\$id\": \"2\", \"StatusCode\": 0,\"Outcome\": \"NewCall\"}}" def myguid = "" def test = new JsonSlurper().parseText(json) println test.keySet()[1]
I would welcome any help/hints/advice anyone has!
thanks to all!
richie
richie, here is one more method to execute a REST step for multiple UIDs. In this example I have used your response with 3 different UIDs.
I saved below response in a text file and reading it from there.
{ "$id" : "1", "1826d9a8-f542-e811-8120-5065f38b0571" : { "$id" : "2", "StatusCode" : 0, "Outcome" : "NewCall" }, "beb19fbb-f542-e811-8120-5065f38b0571" : { "$id" : "3", "StatusCode" : 0, "Outcome" : "NewCall" }, "ffe022cc-f542-e811-8120-5065f38b0571" : { "$id" : "4", "StatusCode" : 0, "Outcome" : "NewCall" } }
groovy script to extract GUIDs and iterate through them and execute RestTestStep for each GUID.
import groovy.json.JsonSlurper def response = new File("c://users//XXXX//desktop//response.txt").text def json = new JsonSlurper().parseText response def i = 0 def UID = [] json.each{ if(it.key !='$id'){ UID += it.key } i++ } log.info UID //## Array with 3 unique IDs ##// def j=0 def uniqueId UID.each{ //##Update TestCase Property with UID ##// uniqueId = UID[j] testRunner.testCase.setPropertyValue("uniqueId", UID[j]) //store in a testcase property and then use this in subsequesnt RestTestStep// log.info "Executing RestStep for UID- $uniqueId" testRunner.runTestStepByName("YourRESTTestStep") // Execute RestStep Here // j++ }