FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Lists from Json
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM

Lists from Json

Posted: Fri Dec 06, 2019 03:55 PM

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 ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM

Re: Lists from Json

Posted: Fri Dec 06, 2019 06:05 PM
Byron, try with this: ( "userFields": [ ( is a array of hash ) )
Look in xbrowse, in "userFields" show {=>}{=>} ( two hashs )
Code (fw): Select all Collapse
#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
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM

Re: Lists from Json

Posted: Fri Dec 06, 2019 10:16 PM

Very cool, thank you for the reply, I will try it.
Makes sense.

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services

Continue the discussion