1. Louis Arena
  2. PowerBuilder
  3. Wednesday, 10 August 2022 20:26 PM UTC

I am trying to parse the return JSON statement from an online credit card company.  The result Json I get back is the following (please note this is from their sandbox using their sample data).

{
"transactionResponse": {
"responseCode": "1",
"authCode": "X4OMIZ",
"avsResultCode": "Y",
"cvvResultCode": "P",
"cavvResultCode": "2",
"transId": "60197979609",
"refTransID": "",
"transHash": "",
"testRequest": "0",
"accountNumber": "XXXX0015",
"accountType": "MasterCard",
"messages": [{
"code": "1",
"description": "This transaction has been approved."
}],
"transHashSha2": "E7F434A59456910F0CF72AAF0A3031C06AA5F28746BF7895B8794A4BA60EA0C7586A2C08259BB78B4E05DAD67242478C53A3FA6600B7B5BBB213304735D5E32B",
"SupplementalDataQualificationIndicator": 0,
"networkTransId": "MFKPNF0T4KGJ82HIEF8VHN6"
},
"refId": "MO2000003-12",
"messages": {
"resultCode": "Ok",
"message": [{
"code": "I00001",
"text": "Successful."
}]
}
}

 

When I use the following code;

JSONPackage ljp_pkg
ljp_pkg = CREATE JSONPackage

// load the entire json result set
ls_ret = ljp_pkg.loadstring(as_result)  // as_result is a string set to the json above
// get the json section for messages only
ls_json = ljp_pkg.getvalue("messages")

At this point ls_json = {"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}

I do not understand why it is not [{"code":"1","description":"This transaction has been approved."}] which is the first instance of the work message in the return Json.  I even tried stripping out the [ ] from the string and it still goes to the second key of message.  The instructions for GetValue state that it will return the first instance.

Any idea?

 

 

John Fauss Accepted Answer Pending Moderation
  1. Thursday, 11 August 2022 16:43 PM UTC
  2. PowerBuilder
  3. # 1

Hi, Louis -

I believe you need to parse each object level separately:

ljp_pkg1 = CREATE JSONPackage
ls_ret1 = ljp_pkg1.LoadString(ls_json)
If ls_ret1 = "" Then
   ls_transresp = ljp_pkg1.GetValue("transactionResponse")
   
   ljp_pkg2 = CREATE JSONPackage
   ls_ret2 = ljp_pkg2.LoadString(ls_transresp)
   If ls_ret2 = "" Then
      ls_messages = ljp_pkg2.GetValue("messages")
      
      ljp_pkg3 = CREATE JSONPackage
      ls_ret3 = ljp_pkg3.LoadString(ls_messages)
      If ls_ret3 = "" Then
         ls_code = ljp_pkg3.GetValue("code")
         ls_desc = ljp_pkg3.GetValue("description")
         MessageBox("Messages","Code: "+ls_code+"~r~n~r~nDescription: "+ls_desc)
      Else
         MessageBox("Object Level 3 Error",ls_ret3)
      End If
   Else
      MessageBox("Object Level 2 Error",ls_ret2)
   End If
Else
   MessageBox("Object Level 1 Error",ls_ret1)
End If

If IsValid(ljp_pkg3) Then DESTROY ljp_pkg3
If IsValid(ljp_pkg2) Then DESTROY ljp_pkg2
If IsValid(ljp_pkg1) Then DESTROY ljp_pkg1

Best regards, John

 

Comment
  1. Louis Arena
  2. Thursday, 11 August 2022 20:29 PM UTC
John, thanks this is exactly what I needed. I was not aware I need to create "sub" jsonpackages and that is why I was stuck.
  1. Helpful
  1. John Fauss
  2. Thursday, 11 August 2022 20:42 PM UTC
I strongly recommend you look at Mike's latest response. I just started using the JSON-related objects today, so I suggest you follow his advice. I wanted to show what might be a possible approach. Good Luck!!!
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Thursday, 11 August 2022 18:21 PM UTC
  2. PowerBuilder
  3. # 2


use jsonparser:

parser.loadstring(string)
ParentItemHandle = parser.GetRootItem()
transHandle = parser.GetItemObject ( ParentItemHandle, 'transactionResponse' )
messagehandle = parser.GetItemObject (transHandle, 'messages' )

etc.
 

Comment
  1. John Fauss
  2. Thursday, 11 August 2022 19:36 PM UTC
Thank you, Mike. I'm very new (< 1 day) getting into JSON, still trying to figure things out...(and the PB Help explains syntax but not context) but it was clear that Louis' original approach needed to be a little more robust. I appreciate you chiming in to assist.
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Wednesday, 10 August 2022 20:48 PM UTC
  2. PowerBuilder
  3. # 3

the value: 

"code": "1",
"description": "This transaction has been approved."

 

is a message object in the transaction object, so you need to get the transaction object first then the message object from that object

Comment
There are no comments made yet.
Louis Arena Accepted Answer Pending Moderation
  1. Wednesday, 10 August 2022 21:16 PM UTC
  2. PowerBuilder
  3. # 4

Can you please explain further.  

what is '"message": [{"code": "I00001","text": "Successful."' 

in the transaction object then?

 

Comment
  1. mike S
  2. Wednesday, 10 August 2022 21:59 PM UTC
you have 3 key/values at the root object and you requested the message value from the root object:



"transactionResponse": { OBJECT },

"refId": "MO2000003-12",

"messages": { OBJECT }



use a json formatter so you can more easily see the hierarchy of the json



https://jsonformatter.org/

  1. Helpful
  1. mike S
  2. Wednesday, 10 August 2022 22:00 PM UTC
you need to get the transaction response node to traverse to the message object you seem to be interested in
  1. Helpful
  1. Louis Arena
  2. Thursday, 11 August 2022 14:42 PM UTC
Ok there are 3 keys, I get that. I have tested the JSONPackage and JSONParser but there is no way I am seeing how I can get the messages section in the 1st key, which is part of the transactionresponse Object. How exactly do I get that information. in the Parser GetChildkey is not finding it.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.