B4J Question Incomplete parsing code with JSON Tree Example

Tim Chapman

Active Member
Licensed User
Longtime User
Using the JSON Tree Example here: http://basic4ppc.com:51042/json/index.html

It gives incomplete code to parse the JSON I am working with.
Any ideas why this is?

My JSON string is in the attached file called JSON String.txt. It is large.

It gives this code:

Incomplete Parsing Code:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim end_date As String = jRoot.Get("end_date")
Dim account_number As String = jRoot.Get("account_number")
Dim last_refreshed_datetime As String = jRoot.Get("last_refreshed_datetime")
Dim links As List = jRoot.Get("links")
For Each collinks As Map In links
 Dim method As String = collinks.Get("method")
 Dim rel As String = collinks.Get("rel")
 Dim href As String = collinks.Get("href")
Next
Dim page As Int = jRoot.Get("page")
Dim total_pages As Int = jRoot.Get("total_pages")
Dim total_items As Int = jRoot.Get("total_items")
Dim transaction_details As List = jRoot.Get("transaction_details")
For Each coltransaction_details As Map In transaction_details
 Dim cart_info As Map = coltransaction_details.Get("cart_info")
 Dim item_details As List = cart_info.Get("item_details")
 For Each colitem_details As Map In item_details
  Dim item_code As String = colitem_details.Get("item_code")
  Dim total_item_amount As Map = colitem_details.Get("total_item_amount")
  Dim value As String = total_item_amount.Get("value")
  Dim currency_code As String = total_item_amount.Get("currency_code")
  Dim item_unit_price As Map = colitem_details.Get("item_unit_price")
  Dim value As String = item_unit_price.Get("value")
  Dim currency_code As String = item_unit_price.Get("currency_code")
  Dim item_name As String = colitem_details.Get("item_name")
  Dim item_amount As Map = colitem_details.Get("item_amount")
  Dim value As String = item_amount.Get("value")
  Dim currency_code As String = item_amount.Get("currency_code")
  Dim item_description As String = colitem_details.Get("item_description")
  Dim item_quantity As String = colitem_details.Get("item_quantity")
 Next
 Dim shipping_info As Map = coltransaction_details.Get("shipping_info")
 Dim address As Map = shipping_info.Get("address")
 Dim country_code As String = address.Get("country_code")
 Dim city As String = address.Get("city")
 Dim state As String = address.Get("state")
 Dim postal_code As String = address.Get("postal_code")
 Dim line1 As String = address.Get("line1")
 Dim method As String = shipping_info.Get("method")
 Dim name As String = shipping_info.Get("name")
 Dim incentive_info As Map = coltransaction_details.Get("incentive_info")
 Dim store_info As Map = coltransaction_details.Get("store_info")
 Dim auction_info As Map = coltransaction_details.Get("auction_info")
 Dim transaction_info As Map = coltransaction_details.Get("transaction_info")
 Dim transaction_id As String = transaction_info.Get("transaction_id")
 Dim shipping_amount As Map = transaction_info.Get("shipping_amount")
 Dim value As String = shipping_amount.Get("value")
 Dim currency_code As String = shipping_amount.Get("currency_code")
 Dim transaction_updated_date As String = transaction_info.Get("transaction_updated_date")
 Dim fee_amount As Map = transaction_info.Get("fee_amount")
 Dim value As String = fee_amount.Get("value")
 Dim currency_code As String = fee_amount.Get("currency_code")
 Dim transaction_status As String = transaction_info.Get("transaction_status")
 Dim transaction_initiation_date As String = transaction_info.Get("transaction_initiation_date")
 Dim transaction_amount As Map = transaction_info.Get("transaction_amount")
 Dim value As String = transaction_amount.Get("value")
 Dim currency_code As String = transaction_amount.Get("currency_code")
 Dim ending_balance As Map = transaction_info.Get("ending_balance")
 Dim value As String = ending_balance.Get("value")
 Dim currency_code As String = ending_balance.Get("currency_code")
 Dim protection_eligibility As String = transaction_info.Get("protection_eligibility")
 Dim transaction_event_code As String = transaction_info.Get("transaction_event_code")
 Dim available_balance As Map = transaction_info.Get("available_balance")
 Dim value As String = available_balance.Get("value")
 Dim currency_code As String = available_balance.Get("currency_code")
 Dim paypal_account_id As String = transaction_info.Get("paypal_account_id")
 Dim payer_info As Map = coltransaction_details.Get("payer_info")
 Dim country_code As String = payer_info.Get("country_code")
 Dim account_id As String = payer_info.Get("account_id")
 Dim email_address As String = payer_info.Get("email_address")
 Dim payer_status As String = payer_info.Get("payer_status")
 Dim payer_name As Map = payer_info.Get("payer_name")
 Dim surname As String = payer_name.Get("surname")
 Dim given_name As String = payer_name.Get("given_name")
 Dim alternate_full_name As String = payer_name.Get("alternate_full_name")
 Dim address_status As String = payer_info.Get("address_status")
Next
Dim start_date As String = jRoot.Get("start_date")

I got this parsing code from OpenAI which as far as I can tell is complete:

Complete Parsing Code:
    Dim parser As JSONParser
    parser.Initialize(combinedJson) ' Replace <text> with your actual JSON input string
   
    Dim jRoot As Map = parser.NextObject
    Dim end_date As String = jRoot.Get("end_date")
    Dim account_number As String = jRoot.Get("account_number")
    Dim page As Int = jRoot.Get("page")
    Dim total_pages As Int = jRoot.Get("total_pages")
    Dim total_items As Int = jRoot.Get("total_items")
   
    Dim links As List = jRoot.Get("links")
    For Each collinks As Map In links
        Dim method As String = collinks.Get("method")
        Dim rel As String = collinks.Get("rel")
        Dim href As String = collinks.Get("href")
    Next
   
    Dim transaction_details As List = jRoot.Get("transaction_details")
    For Each coltransaction_details As Map In transaction_details
        Dim cart_info As Map = coltransaction_details.Get("cart_info")
        Dim item_details As List = cart_info.Get("item_details")
   
        For Each colitem_details As Map In item_details
            Dim item_code As String = colitem_details.Get("item_code")
            Dim item_name As String = colitem_details.Get("item_name")
            Dim item_description As String = colitem_details.Get("item_description")
            Dim item_quantity As String = colitem_details.Get("item_quantity")
            Dim item_unit_price As Map = colitem_details.Get("item_unit_price")
            Dim unitPriceValue As String = item_unit_price.Get("value")
            Dim unitPriceCurrencyCode As String = item_unit_price.Get("currency_code")
            Dim item_amount As Map = colitem_details.Get("item_amount")
           
            Dim total_item_amount As Map = colitem_details.Get("total_item_amount")
            Dim amountValue As String = item_amount.Get("value")
            Dim amountCurrencyCode As String = item_amount.Get("currency_code")
            Dim totalAmountValue As String = total_item_amount.Get("value")
            Dim totalAmountCurrencyCode As String = total_item_amount.Get("currency_code")
        Next
   
        Dim shipping_info As Map = coltransaction_details.Get("shipping_info")
        Dim shippingName As String = shipping_info.Get("name")
        Dim shippingMethod As String = shipping_info.Get("method")
       
        Dim address As Map = shipping_info.Get("address")
        Dim shippingCountryCode As String = address.Get("country_code")
        Dim shippingCity As String = address.Get("city")
        Dim shippingState As String = address.Get("state")
        Dim shippingPostalCode As String = address.Get("postal_code")
        Dim shippingLine1 As String = address.Get("line1")
        Dim shippingLine2 As String = address.Get("line2") ' This line may return null, so handle with care

        Dim transaction_info As Map = coltransaction_details.Get("transaction_info")
        Dim transaction_id As String = transaction_info.Get("transaction_id")
        Dim paypal_account_id As String = transaction_info.Get("paypal_account_id")
        Dim paypal_reference_id As String = transaction_info.Get("paypal_reference_id")
        Dim paypal_reference_id_type As String = transaction_info.Get("paypal_reference_id_type")
        Dim transaction_event_code As String = transaction_info.Get("transaction_event_code")
        Dim transaction_initiation_date As String = transaction_info.Get("transaction_initiation_date")
        Dim transaction_updated_date As String = transaction_info.Get("transaction_updated_date")
   
        Dim transaction_amount As Map = transaction_info.Get("transaction_amount")
        Dim amountValue As String = transaction_amount.Get("value")
        Dim currency_code As String = transaction_amount.Get("currency_code")

        Dim shipping_amount As Map = transaction_info.Get("shipping_amount")
        Dim shippingValue As String = shipping_amount.Get("value")
        Dim shippingCurrencyCode As String = shipping_amount.Get("currency_code")

        Dim fee_amount As Map = transaction_info.Get("fee_amount")
        Dim feeValue As String = fee_amount.Get("value")
        Dim feeCurrencyCode As String = fee_amount.Get("currency_code")

        Dim ending_balance As Map = transaction_info.Get("ending_balance")
        Dim endingValue As String = ending_balance.Get("value")
        Dim endingCurrencyCode As String = ending_balance.Get("currency_code")

        Dim available_balance As Map = transaction_info.Get("available_balance")
        Dim availableValue As String = available_balance.Get("value")
        Dim availableCurrencyCode As String = available_balance.Get("currency_code")

        Dim protection_eligibility As String = transaction_info.Get("protection_eligibility")
        Dim transaction_status As String = transaction_info.Get("transaction_status")
   
        ' Payer Info
        Dim payer_info As Map = coltransaction_details.Get("payer_info")
        Dim payer_country_code As String = payer_info.Get("country_code")
        Dim payer_account_id As String = payer_info.Get("account_id")
        Dim payer_status As String = payer_info.Get("payer_status")
       
        Dim payer_name As Map = payer_info.Get("payer_name")
        Dim payer_given_name As String = payer_name.Get("given_name")
        Dim payer_surname As String = payer_name.Get("surname")
        Dim payer_alternate_full_name As String = payer_name.Get("alternate_full_name")
        Dim payer_email As String = payer_info.Get("email_address")
       
        Dim phone_number As Map = payer_info.Get("phone_number") ' This might be null depending on the result
        If phone_number <> Null Then
            Dim phone_country_code As String = phone_number.Get("country_code")
            Dim national_number As String = phone_number.Get("national_number")
        End If
    Next

    Dim start_date As String = jRoot.Get("start_date")
 
Last edited by a moderator:
Solution
As far as I understand the tool it will extract the fields from the first map in an array. Your first two transactions are missing the PayPal reference id. That’s why it’s missing. I think expects all maps to be the same.

Tim Chapman

Active Member
Licensed User
Longtime User
  • item_description
  • item_quantity
  • paypal_reference_id
  • paypal_reference_id_type
  • phone_number map and its two fields: phone_country_code and national_number
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
As far as I understand the tool it will extract the fields from the first map in an array. Your first two transactions are missing the PayPal reference id. That’s why it’s missing. I think expects all maps to be the same.
 
Upvote 1
Solution

Tim Chapman

Active Member
Licensed User
Longtime User
As far as I understand the tool it will extract the fields from the first map in an array. Your first two transactions are missing the PayPal reference id. That’s why it’s missing. I think expects all maps to be the same.
Thank you. That is very helpful.
 
Upvote 0
Top