Forum Discussion

Raisa's avatar
Raisa
Occasional Contributor
9 years ago

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 e...
  • rupert_anderson's avatar
    9 years ago

    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