Forum Discussion

krispokkuluri's avatar
krispokkuluri
Occasional Contributor
3 years ago

I have a groovy script that parses a complex json and throws the desired values in loop

Hi team 

I have a groovy script that parses a complex json and throws the desired values in loop, My question is How do i use these values in the other test steps .. 

There are to sets of data that i have tried to get  from the json and i have to pass it to another groovy script and sql query and also loop it so that one data is passed after the other now i want to be able to fetch the grantDetailId and so on so forth in the scripts . Kindly Help Thanks In Advance

import groovy.json.*
import groovy.util.*

def response = context.expand( '${REST Request#Response}' )
def parsedJson = new JsonSlurper().parseText(response)

//parsedJson.each {log.info it}

//parsedJson[0].each{k,v->log.info " key : $k,value:$v"}
//parsedJson[0].Grant.each{k,v->log.info "Grant key : $k,value:$v"}
//def GrantDetailId = parsedJson[0].Grant.GrantDetails[1].GrantDetailId.toString()
//log.info GrantDetailId
def cnt =parsedJson[0].Grant.GrantDetails.size
log.info cnt
if (cnt=='1'){//Initial Request details 
					def request = parsedJson[0].findAll{k,v -> v}
					log.info request
					def id = request.$id
					def grantDetailId = request.GrantDetailId
					def grantId = request.GrantId
					def startDate = request.StartDate
					def endDate = request.EndDate
					def qty = request.Quantity
					def leaseRate = request.LeaseRate
					def IsAmortized = request.IsAmortized
					def LicenseTypeId = request.LicenseTypeId
					log.info ("id=$id")
					log.info("GrantDetailId = $grantDetailId")
					log.info("GrantId = $grantId")
					log.info ("StartDate=$startDate")
					log.info ("EndDate=$endDate")
					log.info ("Quantity=$qty")
					log.info("LeaseRate=$leaseRate")
					log.info ("IsAmortized=$IsAmortized")
					log.info ("LicenseTypeId=$LicenseTypeId")
	}
for(i=0;i<cnt;i++){
	if (cnt<1){}
	if (i = cnt-1){//Grant/GrantDetail
						def grantDetailId = parsedJson[0].Grant.GrantDetails[i].GrantDetailId.toString()
						def qty = parsedJson[0].Grant.GrantDetails[i].Quantity.toInteger()
						//Grant/GrantDetail
						def grantId = parsedJson[0].Grant.GrantId.toInteger()
						def startDate = parsedJson[0].Grant.StartDate.toString()
						def endDate =  parsedJson[0].Grant.EndDate.toString()
						def leaseRate = parsedJson[0].Grant.LeaseRate.toString()
						//Grant/project
						def projectId = parsedJson[0].Grant.Project.ProjectId.toString()
						def projectName = parsedJson[0].Grant.Project.ProjectName.toString()
              					 log.info grantDetailId + '  '+ qty.toString()+'  '+startDate+' '+endDate+' '+grantId+'  '+projectId
               					
               					assert qty == 3
						break
				   }
}
//if more grant records are present
//reading grants length
def grantscnt=parsedJson[0].Grant.Project.Grants.size
log.info grantscnt
for(i=0;i<grantscnt;i++){
	if (grantscnt<=1){log.info("There are no grants")
	break
	}
	if (i=grantscnt -1){
	def request2 = parsedJson[0].Grant.Project.Grants[i]
	log.info request2
	//Grants			
						def id	= parsedJson[0].Grant.Project.Grants[i].$id
						def grantId = parsedJson[0].Grant.Project.Grants[i].GrantId.toInteger()
						def startDate = parsedJson[0].Grant.Project.Grants[i].StartDate.toString()
						def endDate =  parsedJson[0].Grant.Project.Grants[i].EndDate.toString()
						def catalogId = parsedJson[0].Grant.Project.Grants[i].CatalogId.toString()
						//log.info id+' '+grantId+' '+startDate+' '+endDate+'  '+catalogId
						def grantDetailcnt = parsedJson[0].Grant.Project.Grants[i].GrantDetails.size()
						log.info(grantDetailcnt)
						for(j=0;j<grantDetailcnt;j++){
							if (j=grantDetailcnt -1){
						  def grantDetaiIs = parsedJson[0].Grant.Project.Grants[i].GrantDetails[j]
						 def grantDetailId = grantDetaiIs.GrantDetailId.toString()
						 def qty = grantDetaiIs.Quantity.toInteger()
						 log.info grantDetailId+' '+qty+' '+id+' '+grantId+' '+startDate+' '+endDate+'  '+catalogId
						break
							}
				
				break		
						}
	}}

//parsedJson[0].Grant.Project.each{k,v->log.info "Project key : $k,value:$v"}

 

2 Replies

  • Ilario72's avatar
    Ilario72
    Occasional Contributor

    Hi,

    first of all, with a groovy script (e.g. step-1) you can set a property value with the size of the result (grantscnt) and save an additional property for take note of actual result index, example:

    def grantscnt = parsedJson[0].Grant.Project.Grants.size;

    testStep.testCase.setPropertyValue("grantscnt", grantscnt);
    testStep.testCase.setPropertyValue("grantindex", "0");

     

    then in the next groovy step (e.g. step-2)  get the values of the first grantDetailId and save as properties and update actual result index:

    def index = testStep.testCase.getPropertyValue("grantindex").toInteger();

    testStep.testCase.setPropertyValue("grantindex", (index +1).toString() );

     

    now you can get the properties values and use as you want (SQL, requests, etc)

    and finally with a groovy script step go to step-2 and get the next index until grantindex<grantscnt using the gotoStepByName function

    • krispokkuluri's avatar
      krispokkuluri
      Occasional Contributor

      Hi Ilario72 ,

      Firstly thanks for your response, So as you can see there are 2 for loops . so do you think i can utilize this approach to get data from the both loops or maybe if possible you can suggest a decision statement to get the values i basically need startdate, endate and qty for a query and another groovy attaching my project for your reference . Can you tell me accordingly . I am really new to groovy and ReadyAPI