Page 1 of 1

Decode JSON - revisited

PostPosted: Wed Feb 15, 2023 12:16 am
by TimStone
This is a JSON response code I have received:

Code: Select all  Expand view
     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

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

Re: Decode JSON - revisited

PostPosted: Wed Feb 15, 2023 9:54 am
by Enrico Maria Giordano
Try this:

Code: Select all  Expand view
? HB_HGET( HB_HGET( hChgData, "transactionResponse" ), "cashier" )

Re: Decode JSON - revisited

PostPosted: Wed Feb 15, 2023 4:59 pm
by AntoninoP
it works with [] operator too:
Code: Select all  Expand view
? hChgData["transactionResponse"]["cashier"]

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

with hashes the $ operator works too:
Code: Select all  Expand view
? "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
{
    "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"
    }
}

Re: Decode JSON - revisited

PostPosted: Wed Feb 15, 2023 7:40 pm
by TimStone
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.