Decode JSON - revisited

Post Reply
User avatar
TimStone
Posts: 2954
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Has thanked: 25 times
Been thanked: 2 times
Contact:

Decode JSON - revisited

Post by TimStone »

This is a JSON response code I have received:

Code: Select all | Expand

      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

// 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
Enrico Maria Giordano
Posts: 8736
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Decode JSON - revisited

Post by Enrico Maria Giordano »

Try this:

Code: Select all | Expand

? HB_HGET( HB_HGET( hChgData, "transactionResponse" ), "cashier" )
AntoninoP
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: Decode JSON - revisited

Post by AntoninoP »

it works with [] operator too:

Code: Select all | Expand

? hChgData["transactionResponse"]["cashier"]
or

Code: Select all | Expand

? hChgData["transactionResponse","cashier"]
with hashes the $ operator works too:

Code: Select all | Expand

? "transactionResponse" $ hChgData
IMHO it is pretty cool
for a better understand of the date I suggest to format the JSON:

Code: Select all | Expand

{
    "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"
    }
}
User avatar
TimStone
Posts: 2954
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA
Has thanked: 25 times
Been thanked: 2 times
Contact:

Re: Decode JSON - revisited

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