cancel
Showing results for 
Search instead for 
Did you mean: 

How to find latest element in json array with Property Transfer

yu
Occasional Contributor

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

Re: How to find latest element in json array with Property Transfer

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" Smiley Happy

 

Thanks and Regards,

Himanshu Tayal

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

Thanks and Regards,
Himanshu Tayal
Highlighted
Moderator

Re: How to find latest element in json array with Property Transfer

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. 

Olga Terentieva,
SmartBear Assistant Community Manager
Watch a Video Interview
Top Kudoed Authors
Are You In?

Meet the Community Leaders!

SmartBear Connect is an excellent opportunity for training, in-person support, and networking.

GET YOUR TICKET WITH $100 OFF