cancel
Showing results for 
Search instead for 
Did you mean: 

Use dynamic index in holder

SOLVED
Highlighted
New Contributor

Use dynamic index in holder

I have a scenario where 1 API returns list response. Response attached for reference.

I need to fetch values of subscriberId, responseDate, device, manufacturer, plan, name for each subscriber and add to property called subscriberInfo.

The output format should be: Subscriber: 123 has date: YYYY-MM-dd device: iphone manufactured by: apple and Plan: airtel and subscriber name is: tom

 

To read the values and transfer to a property I have written following groovy script:

subscriberId = holder["//*:subscriberInfo[1]/*:basicInfo/*:subscriberId"].toString()
responseDate = holder["//*:subscriberInfo[1]/*:basicInfo/*:Date"].toString()
device = holder["//*:subscriberInfo[1]/*:deviceInfo/*:device"].toString()
manufacturer = holder["//*:subscriberInfo[1]/*:deviceInfo/*:manufacturer"].toString()
plan = holder["//*:subscriberInfo[1]/*:plan/*:operator"].toString()
name = holder["//*:subscriberInfo[1]/*:personalInfo/*:name"].toString()

subscriberInfo = "Subscriber: "+subscriberId+ " has date: "+responseDate+" device: "+device+" manufactured by: "+manufacturer+" and Plan: "+plan+" and subscriber name is: "+name

 

I need to repeat these lines for all the subscribers by changing the index number.

 

Tried using below script to not repeat lines for each index:

for(int i=1; i<=subscriberListSize; i++){
log.info i
subscriberId = holder["//*:subscriberInfo[i]/*:basicInfo/*:subscriberId"].toString()
log.info subscriberId
}

but this returns null value. holder does not pick up the value of i.

 

Can someone please help me know if it is possible to iterate over list and pick up values using dynamic index value.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Leader

Re: Use dynamic index in holder

@Shweta_M :

 

That i in you for loop didn't work like you have written, please use as below :

 

for(int i=1; i<=subscriberListSize; i++){
	log.info i
	subscriberId = holder["//*:subscriberInfo"+i.toString()+"/*:basicInfo/*:subscriberId"].toString()
	log.info subscriberId
}

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: Use dynamic index in holder

Please see if the below link helps!
http://groovy-lang.org/processing-xml.html


Regards,
Rao.
Highlighted
New Contributor

Re: Use dynamic index in holder

Hi Rao,

 

I have tried using slurper with findAll as well but that doesnt help either since my nodes are nested within different elements in a list

Highlighted
Community Hero

Re: Use dynamic index in holder

Please show what you have tried.


Regards,
Rao.
Highlighted
Community Leader

Re: Use dynamic index in holder

@Shweta_M :

 

That i in you for loop didn't work like you have written, please use as below :

 

for(int i=1; i<=subscriberListSize; i++){
	log.info i
	subscriberId = holder["//*:subscriberInfo"+i.toString()+"/*:basicInfo/*:subscriberId"].toString()
	log.info subscriberId
}

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

Highlighted
New Contributor

Re: Use dynamic index in holder

This helps !!

Thanks @HimanshuTayal  @nmrao for all the guidance.

Community Leader

Re: Use dynamic index in holder

@Shweta_M :

 

i am Happy to know that it helped 🙂


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors