B4J Question How to pass JSON to map - when inside of it there are more Jsons

Magma

Expert
Licensed User
Longtime User
Well...

Sometimes XML is easier... for example this json has into it more jsons...

B4X:
{
  "state": "SUCCESS",
  "sessionType": "SALE",
  "sessionId": "4bdebe62-c211-4ca0-a994-b2fbea2061c5",
  "payloadData": {
    "saleResponse": {
      "transactionId": "580e0bfc-ad68-420f-af02-2900fb74496a",
      "tid": "16031291",
      "verificationMethod": "CONTACTLESS - NO CVM",
      "retrievalReferenceNumber": 206922748800,
      "transactionDate": "2022-03-11 19:34:58.0163 +02:00",
      "authorizationId": "266207",
      "panEntryMode": "07",
      "aid": "Identifier",
      "tipAmount": 50,
      "amount": 1220,
      "isAbortRequested": false,
      "applicationLabel": "VISA Debit",
      "primaryAccountNumberMasked": "479275XXXXXX9999",
      "referenceNumber": 325495,
      "orderCode": "2329122555031291",
      "shortOrderCode": "2328182546",
      "installments": 0,
      "eventId": 10051,
      "message": "Transaction successful",
      "isSuccess": true,
      "transactionTypeId": 5,
      "merchantApproved": true,
      "aadeTransactionId": "116123456833121690882"
    }
  }
}

The basic json ---> payloaddata ----> saleResponse (or abortResponse if different response), going 2 step more...

Thought was easy but when reading as map, removing quotes... then thought replaces double QUOTEs with single QUOTE... but the same...

here my code failing to do the job - i need:

B4X:
        Dim jsonstring As String=j.GetString.As(String).Replace(QUOTE,"'")
        Log(jsonstring)
        Dim m As Map = jsonstring.As(JSON).ToMap 'ignore
        state=m.Get("state")
        sType=m.Get("sessionType")
        
        Dim atext As String=m.get("payloadData").As(String)
        Log("----")
        Log(atext)
        
        If atext.Trim.Length>0 And atext.trim<>"null" Then
            Dim payloaddata As Map = atext.As(JSON).ToMap
            
            Dim btext As String=payloaddata.Get("abortResponse").As(String)
            If btext.Trim.Length>0 And btext.trim<>"null" Then
                Dim abortresponse As Map = btext.As(JSON).ToMap
                isSuccess=abortresponse.Get("isSuccess")
            End If
            
            Dim ctext As String=payloaddata.Get("saleResponse").As(String)
            If ctext.Trim.Length>0 And btext.trim<>"null" Then
                Dim saleresponse As Map = ctext.As(JSON).ToMap
                transactionId=saleresponse.Get("transactionId")
                aadeTransactionId=saleresponse.get("aadeTransactionId")
                isSuccess=saleresponse.Get("isSuccess")
                isAbortRequested = saleresponse.Get("isAbortRequested")
                message=saleresponse.Get("message")
            End If
            
            
        End If

I am sure if lose some hours i will find the solution... but already lost 2 days :-( and time passing !

any idea ?

thanks in advance
 

drgottjr

Expert
Licensed User
Longtime User
B4X:
    Dim jsonstring As String = $"{
  "state": "SUCCESS",
  "sessionType": "SALE",
  "sessionId": "4bdebe62-c211-4ca0-a994-b2fbea2061c5",
  "payloadData": {
    "saleResponse": {
      "transactionId": "580e0bfc-ad68-420f-af02-2900fb74496a",
      "tid": "16031291",
      "verificationMethod": "CONTACTLESS - NO CVM",
      "retrievalReferenceNumber": 206922748800,
      "transactionDate": "2022-03-11 19:34:58.0163 +02:00",
      "authorizationId": "266207",
      "panEntryMode": "07",
      "aid": "Identifier",
      "tipAmount": 50,
      "amount": 1220,
      "isAbortRequested": false,
      "applicationLabel": "VISA Debit",
      "primaryAccountNumberMasked": "479275XXXXXX9999",
      "referenceNumber": 325495,
      "orderCode": "2329122555031291",
      "shortOrderCode": "2328182546",
      "installments": 0,
      "eventId": 10051,
      "message": "Transaction successful",
      "isSuccess": true,
      "transactionTypeId": 5,
      "merchantApproved": true,
      "aadeTransactionId": "116123456833121690882"
    }
  }
}"$

Dim jparser As JSONParser
jparser.Initialize(jsonstring)
Dim topelement As Map = jparser.NextObject
Dim payload As Map = topelement.Get("payloadData")
Dim saleResponse As Map = payload.Get("saleResponse")

Dim message As String =saleResponse.Get("message")
Dim amount As String = saleResponse.Get("amount")
Log("amount: " & amount & " message: " & message)

or if you want to save a couple of linefeeds:
Dim saleResponse As Map = topelement.Get("payloadData").As(Map).Get("saleResponse")
same difference, same answer.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Once you are familiar with the concept of JSON and Map, you can loop the object with For Each loop.

B4X:
Sub ParseJSON2
    Dim jsonMap As Map = jsonstring.As(JSON).ToMap
    For Each Key As String In jsonMap.Keys
        LogColor($"${Key}: ${jsonMap.Get(Key)}"$, xui.Color_Red)
        If Key = "payloadData" Then
            Dim payloadData As Map = jsonMap.Get("payloadData")
            For Each Key As String In payloadData.Keys
                LogColor($"  ${Key}: ${payloadData.Get(Key)}"$, xui.Color_Magenta)
                If Key = "saleResponse" Then
                    Dim saleResponse As Map = payloadData.Get("saleResponse")
                    For Each Key As String In saleResponse.Keys
                        LogColor($"    ${Key}: ${saleResponse.Get(Key)}"$, xui.Color_Blue)
                    Next
                End If
            Next
        End If
    Next
End Sub

 

Attachments

  • ParseJson2.zip
    9.4 KB · Views: 105
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
If Key = "payloadData" Then
Just curios, is there any way to know that this key will be another json, without hard code of key name ?
i.e something like, if Key.Json() or IsJson(Key) etc.

Is Yes, then it will become very simple to parse nested Json.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…