Android Question Capitalized json key name causes incorrect null value

toby

Well-Known Member
Licensed User
Longtime User
I originally discovered this problem in B4J and reproduced in B4A.

Steps to reproduce the problem: Run the included code in either B4A or B4J.

test example:
    'capitalized keyname: Test
    Dim strJson As String=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","Test":"1"},
                             {"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","Test":"1"}]"$
    test(strJson) 'Test=null  while 1 is expected
    
    'lowercase key name: test
    strJson=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","test":"1"},
               {"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","test":"1"}]"$
    test(strJson) 'Test=1  as expected


B4X:
Private Sub test(strJson As String)
    
    Dim parser As JSONParser
    'parser.Initialize($"${J.GetString}"$)
    parser.Initialize($"${strJson}"$)
    LogDebug("strJson=" & strJson)

    Dim lstResult As List
    lstResult.Initialize
    Try
        lstResult=parser.NextArray
    Catch
        Log(LastException)
        Return
    End Try

    For Each m As Map In lstResult 'ignore
        'go throught each record
        '''''''
        
        Log("***: " & m & " ***")
        Dim msg As String=$"orderItem_id=${m.Get("orderitem_id")}"$
        Log(msg)
        msg=$"qty=${m.Get("qty")}"$
        Log(msg)
        msg=$"amt=${m.Get("amt")}"$
        Log(msg)
        
        msg=$"Test=${m.Get("test")}"$
        Log(msg)
        
    Next
End Sub
 
Solution
Make sure identical key, both in json string and while going through the list, is used, then everything would work.

toby

Well-Known Member
Licensed User
Longtime User
It works if the key is in lowercase and I have to use lowercase key ("test", not "Test") to be able to retrieve the test value in question.

For json string containing "Test":1, m.get.("Test") returns null while 1 is expected. something wrong here.
For json string containing "test":1, m.get.("test") returns 1 as expected.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Make sure identical key, both in json string and while going through the list, is used, then everything would work.
 
Upvote 0
Solution

TILogistic

Expert
Licensed User
Longtime User
?
validate map key
ContainsKey
B4X:
    Dim strJson As String
    strJson=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","Test":"1"},
               {"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","Test":"1"},
               {"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","test":"1"},
               {"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","test":"1"}]"$
   
    Log("keyname: Test or test")
    For Each m As Map In strJson.As(JSON).ToList
        Log(IIf(m.ContainsKey("test"), m.Get("test"), m.Get("Test")))
    Next

or
GetDefault
B4X:
    Log("keyname: Test or test")
    For Each m As Map In strJson.As(JSON).ToList
        Log(m.GetDefault("test", m.Get("Test")))
    Next
1662781106771.png
 
Upvote 0
Top