cancel
Showing results for 
Search instead for 
Did you mean: 

Passing a json node (rather than the node's value) to a Property

SOLVED
Community Hero

Re: Passing a json node (rather than the node's value) to a Property

Hey!

 

ok - when I run the json logging it returns the string 

 

Fri Apr 20 16:44:39 BST 2018:INFO:{$id=1, ee104a67-013f-e811-8122-5065f38be511={$id=2, Outcome=NewCall, StatusCode=0}}

I tried changing the import declaration and the code to use the jsonUtil rather than the jsonSlurper and I got a nullPointer exception - I've asked a developer to look at the code - if its generating a nullPointer theres something seriously wrong with the script and I just can't see what the problem is anymore!

 

The script I've got is as follows:

 

import com.eviware.soapui.support.JsonUtil

def responseCntent = new File("d:/NCC Teleperformance/groovysoapuitestfolder/response1.txt").text 

//def responseCntent = context.expand( '${REST Request - GET TP EOD Results#Response}' )

def json = JsonUtil.parseTrimmedText (responseCntent)

def jNode
def jValue

json.each{
           if(it.key.contains("GUID")){
 	                                  log.info it.key
                                          log.info it.value.$id
 
                                          jNode = it.key
                                          jValue = it.value.$id
                                          context.testCase.testSuite.setPropertyValue("IDName", "$jNode")
                                          context.testCase.testSuite.setPropertyValue("IDVal", "$jValue") 

     }
}
log.info(json)

log.info "$jNode"
log.info "$jValue"

//getting values from test Suite
log.info "Property Value from TestSuite IDName: " + context.expand('${#TestSuite#IDName}')
log.info "Property Value from TestSuite IDVal: " + context.expand('${#TestSuite#IDVal}')

 

Thanks for all your help New2API - really appreciate all the help you've given me!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Contributor

Re: Passing a json node (rather than the node's value) to a Property

Oh boy!!! Richie, your json node is itself is a unique key... not with 'GUID' text as I imagined. So obviously if statement returns false hence it will not do any further processing within IF block. your developer did point you in the right direction in the first place. Btw... is your response will always have 2 json nodes and unique id would be always second one?

Community Hero

Re: Passing a json node (rather than the node's value) to a Property

Hey!

 

Sorry - if I wasn't clear in my description before - when I was using UniqueAlphaNumericGUID - I meant the GUID value such as below.

 

{
	"$id": "1",
	"ee104a67-013f-e811-8122-5065f38be511": {
		"$id": "2",
		"StatusCode": 0,
		"Outcome": "NewCall"
	},
	"466c0c74-013f-e811-8122-5065f38be511": {
		"$id": "3",
		"StatusCode": 0,
		"Outcome": "NewCall"
	}
}

 

Each phonecall record has the GUID (unique primary key) of the phone call as the node name for the json object that contains additional attributes and values for that phone call.  Ive never seen .json do this before - but this is an interface to a 3rd party component - so I can't get the .json response altered - which is why I'm going through all this pain!

 

There will be only one instance of 

"$id": "1",

in the .json response, but there can be 1 or many phone call records 

 

3 phone call records in the database return the following

 

{
   "$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"
   }
}

 

So in regards to your question - there are can be 1 record or many records in the .json response.  The GUID is the first node for the record for ALL the records except the first record - the first record has a parent node of 

"$id": "1",

 

 

The first node for every record EXCEPT the first record is the GUID of the phone call record.

 

Have I explained properly now?

 

thanks man,

 

richie 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: Passing a json node (rather than the node's value) to a Property

@New2API

 

I can't actually understand part of the code you supplied - is there any chance you could explain what the following is actually doing please?  (just for my education)

 

def responseCntent = context.expand( '${REST Request - GET TP EOD Results#Response}' )

def json = new JsonSlurper().parseText responseCntent

def jNode
def jValue

json.each{
if(it.key.contains("GUID")){
log.info it.key
log.info it.value.$id

jNode = it.key
jValue = it.value.$id

 

Its the 'json each' 'if' bits I don't understand

 

thanks,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Contributor

Re: Passing a json node (rather than the node's value) to a Property

@richie, thank you for explaining... let me see if I can modify my script accordingly.

Contributor

Re: Passing a json node (rather than the node's value) to a Property

@richie, json.each similar to for for statement. It loops through the entire data set in that array. for example; json.each is same as for(int i = 0; i<json.size(); i++).

 

if statement: since json is a collection of Key and values, I was specifically looking for a key with a string "GUID". if you execute log.info it then it will print key:value.  if you execute  log.info it.key it will just display key or say json node.

 

hope I was able to clarify your question. 

Contributor

Re: Passing a json node (rather than the node's value) to a Property

@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++
}

View solution in original post

Community Hero

Re: Passing a json node (rather than the node's value) to a Property

@New2API

 

WOW! - thanks so much!

 

Really appreciate all the effort you made on my behalf!

 

richie!

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.

View solution in original post

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors