cancel
Showing results for 
Search instead for 
Did you mean: 

Get the id of a parent element by a sub-child value of Json response using groovy

SOLVED
Highlighted
Contributor

Get the id of a parent element by a sub-child value of Json response using groovy

I have following groovy script to get the values from response text. 

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper 

def response = context.expand( '${GetLoansList#Response}' ).toString()
log.info(response)

def slurper = new JsonSlurper()
def json = slurper.parseText response

log.info(json.items.id)

my json is similar to this 

{
   "items" : [
      {
         "id" : 48223,
         "name" : "LAI-00151007",
         "amount" : 25050.0,
         "interest_rate" : 25.99,
         "term" : 60,
      },
      {
         "id" : 48262,
         "name" : "LAI-00152581",
         "amount" : 44225.0,
         "interest_rate" : 18.9,
         "term" : 36,
      },
   ],
   "total_count" : 13
}

I want to get the corresponding ID for the given name ("name": "LAI-00152581",). What is the best way to do this? Thanks 




1 ACCEPTED SOLUTION

Accepted Solutions
Super Contributor

Re: Get the id of a parent element by a sub-child value of Json response using groovy

@mpw83  - This is how you can go with it :-

 

import groovy.json.JsonSlurper

def json = '''{
   "items" : [
      {
         "id" : "48223",
         "name" : "LAI-00151007",
         "amount" : "25050.0",
         "interest_rate" : "25.99",
         "term" : "60",
      },
      {
         "id" : "48262",
         "name" : "LAI-00152581",
         "amount" : "44225.0",
         "interest_rate" : "18.9",
         "term" : "36",
      }
   ],
   "total_count" : "13"
}'''

def jsonSlurper = new JsonSlurper()
def obj = jsonSlurper.parseText(json)

obj.items.each{
	if(it.name == 'LAI-00152581'){
		log.info it.id
	}
}

@nmrao  - You can also suggest better code.

4 REPLIES 4
Super Contributor

Re: Get the id of a parent element by a sub-child value of Json response using groovy

@mpw83  - This is how you can go with it :-

 

import groovy.json.JsonSlurper

def json = '''{
   "items" : [
      {
         "id" : "48223",
         "name" : "LAI-00151007",
         "amount" : "25050.0",
         "interest_rate" : "25.99",
         "term" : "60",
      },
      {
         "id" : "48262",
         "name" : "LAI-00152581",
         "amount" : "44225.0",
         "interest_rate" : "18.9",
         "term" : "36",
      }
   ],
   "total_count" : "13"
}'''

def jsonSlurper = new JsonSlurper()
def obj = jsonSlurper.parseText(json)

obj.items.each{
	if(it.name == 'LAI-00152581'){
		log.info it.id
	}
}

@nmrao  - You can also suggest better code.

Contributor

Re: Get the id of a parent element by a sub-child value of Json response using groovy

yes, this is worked. Thanks 

Community Hero

Re: Get the id of a parent element by a sub-child value of Json response using groovy

Thats ok @avidCoder .  It takes time to optimize. But,yes can simplifed as below entire script:

 

def json = new groovy.json.JsonSlurper().parseText(context.expand( '${GetLoansList#Response}' ))
log.info json.items.find{it.name = 'LAI-00152581'}.id


Regards,
Rao.
Community Hero

Re: Get the id of a parent element by a sub-child value of Json response using groovy

Thats ok @avidCoder .  It takes time to get into optimization. But,yes it can simplifed as below entire script:

 

def json = new groovy.json.JsonSlurper().parseText(context.expand( '${GetLoansList#Response}' ))
log.info json.items.find{it.name = 'LAI-00152581'}.id

 



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors