Page 1 of 1

Lists from Json

Posted: Fri Dec 06, 2019 3:55 pm
by byron.hopp
How do you read an item from a list in a hash created from a json response.
Look for the "[" in the example response below:

If I used hb_JsonDecode( cJsonResponse,@o )

Then how do I get the second set of variables from the hash?

o["transactionResponse"]["userFields"]["name"]

How do I get to the second set of "name/value" in this hash.

{
"transactionResponse": {
"responseCode": "1",
"authCode": "C1E3I6",
"avsResultCode": "Y",
"cvvResultCode": "S",
"cavvResultCode": "9",
"transId": "2149186775",
"refTransID": "",
"transHash": "C85B15CED28462974F1114DB07A16C39",
"accountNumber": "XXXX0015",
"accountType": "Mastercard",
"messages": [
{
"code": "1",
"description": "This transaction has been approved."
}
],
"userFields": [
{
"name": "MerchantDefinedFieldName1",
"value": "MerchantDefinedFieldValue1"
},
{
"name": "favorite_color",
"value": "blue"
}
]
},
"refId": "123456",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}

Thanks,

Byron ...

Re: Lists from Json

Posted: Fri Dec 06, 2019 6:05 pm
by cnavarro
Byron, try with this: ( "userFields": [ ( is a array of hash ) )
Look in xbrowse, in "userFields" show {=>}{=>} ( two hashs )

Code: Select all

#include "Fivewin.ch"

//----------------------------------------------------------------------------//
// If I used hb_JsonDecode( cJsonResponse,@o )
// Then how do I get the second set of variables from the hash?
// o["transactionResponse"]["userFields"]["name"]
//----------------------------------------------------------------------------//

Function Main()

   Local cText
   Local hHash

   TEXT INTO cText
   {
    "transactionResponse": {
    "responseCode": "1",
    "authCode": "C1E3I6",
    "avsResultCode": "Y",
    "cvvResultCode": "S",
    "cavvResultCode": "9",
    "transId": "2149186775",
    "refTransID": "",
    "transHash": "C85B15CED28462974F1114DB07A16C39",
    "accountNumber": "XXXX0015",
    "accountType": "Mastercard",
    "messages": [
                  {
                  "code": "1",
                  "description": "This transaction has been approved."
                  }
    ],
    "userFields": [
                  {
                  "name": "MerchantDefinedFieldName1",
                  "value": "MerchantDefinedFieldValue1"
                  },
                  {
                  "name": "favorite_color",
                  "value": "blue"
                  }
      ]
   },
   "refId": "123456",
   "messages": {
      "resultCode": "Ok",
      "message": [
                  {
                  "code": "I00001",
                  "text": "Successful."
                  }
       ]
      }
   }
   ENDTEXT

   hb_JsonDecode( cText, @hHash )
   XBrowse( hHash )
   ? hHash["transactionResponse"]["userFields"][ 1 ]["name"]
Return nil
 

Re: Lists from Json

Posted: Fri Dec 06, 2019 10:16 pm
by byron.hopp
Very cool, thank you for the reply, I will try it.
Makes sense.

Byron ...