Forum Discussion

yu's avatar
yu
Occasional Contributor
6 years ago

How to find latest element in json array with Property Transfer

Hi all, 

 I have responce form GET request in Json like: 

[
{
"CompanyID": 374982,
"CompanyStatus": 10,
"IsKeyContact": true,
"IsAuthorisedSignatory": true,
"SubscriptionStatus": 0,
"TelephoneWork": {
"ContactID": 551205,
"CommunicationMethodTypeID": 1,
"CommunicationMethodValue": "",
"ContactCommunicationsID": 1580166,
"IsPrimary": true,
"CommunicationTypeEnum": 1,
"CommunicationTypeName": "Telephone (Work)",
"IsEmail": false,
"IsPhone": true,
"UsedAsOsEmail": null,
"IsTPS": false
}, 

{
"CompanyID": 374982,
"CompanyStatus": 10,
"IsKeyContact": false,
"IsAuthorisedSignatory": false,
"SubscriptionStatus": 0,
"TelephoneWork": {
"ContactID": 551206,
"CommunicationMethodTypeID": 1,
"CommunicationMethodValue": "",
"ContactCommunicationsID": 1580151,
"IsPrimary": true,
"CommunicationTypeEnum": 1,
"CommunicationTypeName": "Telephone (Work)",
"IsEmail": false,
"IsPhone": true,
"UsedAsOsEmail": null,
"IsTPS": false

]

And quantity of elements in the array is defferent time to time. 

I'm using Property Transfer with script $..ContactID[0] to define first element  "ContactID": 551206 and past to property. 

How is possible to define latest ContactID ? 

script $..ContactID[last()] doesn't work! 

 

2 Replies

  • Hi yu,

     

    Yes you can achieve this

    Step 1. Get the count of Arrays in your response within {}, store this count in some variable

    ${TestCaseStep#ResponseAsXml#count(//*:ContectID)}

     

    Step 2. Pass that count to your index within $..ContactID["Count which you get from step 1"].

     

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

     

    Thanks and Regards,

    Himanshu Tayal

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

       

      HimanshuTayal, many thanks for looking into this!

       

      yu, does the solution provided above help you? Did you have a chance to try it?
      If it answers your question, could you please click the Accept as Solution button below the reply that provides the answer? This way, other users will find it easier when searching for an answer to a similar issue in the future.

       

      Thank you in advance.