Decode JSON - revisited

Decode JSON - revisited

Postby TimStone » Wed Feb 15, 2023 12:16 am

This is a JSON response code I have received:

Code: Select all  Expand view  RUN
     cRetData := {"transactionResponse":{"accountCardType":"VS","accountEntryMethod":"PinpadKeyed","accountExpiryDate":"XXXX","amount":"1.55","amountBalance":"","amountProcessed":"1.55","amountTaxed":"0.00","amountTipped":"0.00","approvalNumberResult":"OK6875","avsResponseCode":"","avsResponseText":"","batchNumber":"0","billingName":"","cashier":"","createdOn":"2023-02-15T00:04:35.813Z","customerId":"","cvvResponseCode":"","cvvResponseText":"","externalTransactionId":"f0270891-08da-4b1b-9a7d-5863f490b7d9","isPartialApproval":false,"maskedAccount":"************9016","resultMessage":"Approved","resultStatus":"true","transactionReference":"","transactionType":"CreditSale","uniqueTransId":"f1766ec228104c71a55ce0cd84ddaf72-00000000000000000000000000000000"}}   
 


With other web responses I Decode the result ( into a hash ), and then use HB_HGET( ) to obtain the results.
Code: Select all  Expand view  RUN

// Obtain and save the results   -or-   Parse the results by line
hb_JsonDecode( cRetData, @hChgData )

XBROWSER HASHTREE( hChgData, 0 ) TITLE "Hash as Tree"
             
// Parse the results
cTermID     := HB_HGET( hChgData, "cashier")            // Terminal ID               
cRefCode    := HB_HGET( hChgData, "maskedAccount")  // Credit card number                  
fccddat := HB_HGET( hChgData, "createdOn")      // Transaction date  full string  2016-08-25T15:28:11-05:00  C  8
 


The browser works fine. However I need the data in that 2nd element of the array to be in the hash table ( "cashier" ) and it doesn't find it in the array because the first element ( apparently ) "transactionResponse" is what is being hashed by JsonDecode.

I'm sure there is a solution ... probably a simple modification.

Thanks.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2945
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Decode JSON - revisited

Postby Enrico Maria Giordano » Wed Feb 15, 2023 9:54 am

Try this:

Code: Select all  Expand view  RUN
? HB_HGET( HB_HGET( hChgData, "transactionResponse" ), "cashier" )
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Decode JSON - revisited

Postby AntoninoP » Wed Feb 15, 2023 4:59 pm

it works with [] operator too:
Code: Select all  Expand view  RUN
? hChgData["transactionResponse"]["cashier"]

or
Code: Select all  Expand view  RUN
? hChgData["transactionResponse","cashier"]

with hashes the $ operator works too:
Code: Select all  Expand view  RUN
? "transactionResponse" $ hChgData

IMHO it is pretty cool
for a better understand of the date I suggest to format the JSON:
Code: Select all  Expand view  RUN
{
    "transactionResponse": {
        "accountCardType": "VS",
        "accountEntryMethod": "PinpadKeyed",
        "accountExpiryDate": "XXXX",
        "amount": "1.55",
        "amountBalance": "",
        "amountProcessed": "1.55",
        "amountTaxed": "0.00",
        "amountTipped": "0.00",
        "approvalNumberResult": "OK6875",
        "avsResponseCode": "",
        "avsResponseText": "",
        "batchNumber": "0",
        "billingName": "",
        "cashier": "",
        "createdOn": "2023-02-15T00:04:35.813Z",
        "customerId": "",
        "cvvResponseCode": "",
        "cvvResponseText": "",
        "externalTransactionId": "f0270891-08da-4b1b-9a7d-5863f490b7d9",
        "isPartialApproval": false,
        "maskedAccount": "************9016",
        "resultMessage": "Approved",
        "resultStatus": "true",
        "transactionReference": "",
        "transactionType": "CreditSale",
        "uniqueTransId": "f1766ec228104c71a55ce0cd84ddaf72-00000000000000000000000000000000"
    }
}
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: Decode JSON - revisited

Postby TimStone » Wed Feb 15, 2023 7:40 pm

Thank you both for your responses.

Enrico, your solution worked perfectly. JSON is so widely used now we all need to understand it fully. I find companies, however, output their responses in various JSON formats, so I will be studying the principles more closely.

Antonino, I appreciate those alternative formats and agree with you completely. Also, I do have the formatted response as you suggested and should have used that in my post.

One of the time consuming tasks in decoding JSON responses is the need to compare the type of the value returned and then look at it relative to how it will be applied in the application. For example, some people return all values as type string, and thus numerics must be converted. It's easy enough to do, but everyone seems to handle it differently.

Again, thanks for the answers. They are greatly appreciated.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2945
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 47 guests