Forum Discussion

Shweta_M's avatar
Shweta_M
New Contributor
4 years ago
Solved

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.

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

     

6 Replies

    • Shweta_M's avatar
      Shweta_M
      New Contributor

      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

      • nmrao's avatar
        nmrao
        Champion Level 3
        Please show what you have tried.
  • 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
    }