0 Replies Latest reply on Jul 12, 2011 1:57 PM by ch0c0halic

    Parsing data using a Custom Function

    ch0c0halic

      Data comes in all formats. If the data has a consistent format it can be parsed. The purpose of this customer function is to return a specific fields value from the reply. This could be done in each field but using a custom function allows for easier maintenance. The sample data shows separate lines for each name-value pair. But the data could be a continuous string. In that case use a substitute command to add a return at appropriate places in preparing the string for parsing.

       

      The key is identifying the consistent pattern and using that to locate the desired data.

       

      Custom Function to parse field data from a text string reply:

       

      /*

      get_field_data ( text_reply ; field_name )

      Takes the Text Reply and a field name and returnes the data for the specified field.

      */

       

      Let ( [

      fieldname = field_name ;

      field_row = MiddleValues ( text_reply ; PatternCount ( Left ( text_reply ; Position ( text_reply ; fieldname ; 1 ; 1 ) ) ; "¶" ) + 2 ; 1 ) ;

      row_length = Length ( field_row ) - 1 ;

      left_quote = Position ( field_row ; """ ; 1 ; 1 ) ;

      right_quote = Position ( field_row ; """ ; 2 ; 1 ) ;

      field_text = Middle ( field_row ; left_quote + 1 ; row_length - left_quote - Case ( left_quote > 0 ; 1 ; 0 ) )

      ] ;

      Case (

      PatternCount ( text_reply ; "reject" ) > 0 or

      PatternCount ( text_reply ; fieldname ) = 0 ;

      "" ;

      field_text

      )

      )

       

      Example Data of a rejected Authorization request:

       

      object(stdClass)#10 (7) {

      ["merchantReferenceCode"]=>

      string(11) "123456"

      ["requestID"]=>

      string(22) "563218905684392758423"

      ["decision"]=>

      string(6) "REJECT"

      ["reasonCode"]=>

      int(101)

      ["missingField"]=>

      string(19) "c:billTo/c:lastName"

      ["requestToken"]=>

      string(68) "AdnklfejhiFDKLFxnkldhgFNDShfdhsCNKSAH"

      ["bankTransferReply"]=>

      object(stdClass)#8 (1) {

      ["reasonCode"]=>

      int(101)

      }

      }

       

       

      Using the CF to return a value. Where the field "text_data" containing our sample data is passed into the CF.

       

      get_field_data ( text_data ; "decision" )

       

      will return the value: REJECT