Forum Discussion

Raisa's avatar
Raisa
Occasional Contributor
9 years ago
Solved

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

     

7 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Would it be OK to post a small sample of the response (XML or JSON?) that contains an example of what you want to extract based on the condion you mentioned?

     

    As you say, my first thought is to parse the response and extract the required data using some kind of Groovy script.

     

    regards,

    Rupert

    • Raisa's avatar
      Raisa
      Occasional Contributor

      Hi, thank for response!

      Here is the part of my json sample:

      [...{

      "CardIssueCde": 3,
      "CardLast4Digits": "0660",
      "CardProxyNumber": "1100124371170660",
      "CardStatusCode": 1,
      "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": ""

      }..]

      • Raisa's avatar
        Raisa
        Occasional Contributor

        So, all I need to transfer CardProxyNumber, but only for card with CardStatusCode=1;