Forum Discussion

krispokkuluri's avatar
Occasional Contributor
2 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 { it}

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

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


2 Replies

  • Ilario72's avatar
    Occasional Contributor


    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
      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