Raisa
9 years agoOccasional Contributor
Transfer response data
Hi,
Could you please help me with not trivial transfering data from response.
I have a response with a lot of fields with the same name.
for instance "$[x].CardProxyNumber" where x- number of existing,( now it is about 200 and will be more in future).
So, each Card has status, and only one has status=1;
I need to transfer CardProxyNumber whose status =1 for the next tests (it is always changed, it can be$[2].CardProxyNumber in one test and $[152].CardProxyNumber in the next test).
I think it can be some grrovy script but I'm not sure w to realize it.
Thanks in advance!
Hi,
Ok - how about this:
def response ='''[{ "CardIssueCde": 3, "CardLast4Digits": "0660", "CardProxyNumber": "1100124371170660", "CardStatusCode": 0, "DependentId": "DepId", "EmployeeId": "phase4001", "FirstName": "first name", "LastName": "last name", "MailedDate": " ", "MiddleInitial": "T", "NamePrefix": "" }, { "CardIssueCde": 3, "CardLast4Digits": "0678", "CardProxyNumber": "1100124371180678", "CardStatusCode": 1, "DependentId": "DepId123", "EmployeeId": "phase4001", "FirstName": "first name", "LastName": "last name", "MailedDate": " ", "MiddleInitial": "T", "NamePrefix": "" }]''' import static com.jayway.jsonpath.JsonPath.parse def selectedCardProxyNumber = parse(response).read('$..[?(@.CardStatusCode == 1)].CardProxyNumber') log.info selectedCardProxyNumber[0]
Notes:
- Uses built in JsonPath library
- The JsonPath expression should select ALL CardProxyNumber objects values (into an array) for objects that have a CardStatusCode=1
- It will select CardProxyNumbermore than one if more than 1 object exists with CardStatusCode=1, so in the example I have taken the first element of the array.
Regards,
Rup