krispokkuluri
3 years agoOccasional Contributor
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"}