B4J Question How to know if string is parseable as JSON "list" or "Map"

jmon

Well-Known Member
Licensed User
Longtime User
Hi,

I would like to be able to determine if a JSON string will be parseable as a LIST or as a MAP before processing it.

Am I missing something, or is the only way to know is to test the first character?
"[" = LIST
"{" = MAP

code:
Dim json As JSONParser
json.Initialize(StdOut)

If json.IsInitialized Then
    If StdOut.StartsWith("[") Then
        For Each Program As Map In json.NextArray
            'processing as a list of map
        Next

    Else
        Dim Program As Map = json.NextObject
        'processing as a map

    End If        
End If

Any other idea?

Thanks
 

TILogistic

Expert
Licensed User
Longtime User
That is how you look at it I guess. If it is an error, in my view, it can not be valid so a 'valid error' does not exist, it is just an error and badly formed Json. That is why you need the try catch.
Estimate,
We have routines to validate the Json and Xml, since we capture json structures from different entities or companies.

and the exposed idea is to check if the structure of the Json (Root) is valid and transform it into a single type of variable to process.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
We have routines to validate the Json and Xml, since we capture json structures from different entities or companies.
Same here. We have several connections between e.g. our REST API and several other ones, all exchanging data with json. Our approach is that we do not try to 'fix' bad json. We pass it on to the vendor/client and they have to fix their code so they provide valid json. Just a different approach as we want to tackle the problem at its root.
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
Don’t forget this everyone;

 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Upvote 0
Top