B4J Question Try to produce a json string

codie01

Active Member
Licensed User
Longtime User
Hi All,

To pass data via API i am required to produce a json string as follows:

"objects": [
{
"id": "#56765DFSG8219RR",
"type": "COLLECTION",
"catalog_data": {},
"item_data": {},
"is_deleted": false,
"item_data": {
"seen_globally": true
}
},
{
"id": "#KK165DFSG8219RR",
"type": "COLLECTION",
"catalog_data": {},
"item_data": {},
"is_deleted": false,
"item_data": {
"seen_globally": true
}
]

The issue I have is that object is a List and in normal cases would produce 0,1,2,3 etc for the map objects in the list. In the case of this API it is rejected.

If I place the data in a single map it gets over written each loop based on the keys!

Any ideas on how to produce the example above, thanks.

Philip
 

aeric

Expert
Licensed User
Longtime User
B4X:
    Dim map1 As Map = CreateMap("id": "#56765DFSG8219RR", _
    "type": "COLLECTION", _
    "catalog_data":    CreateMap(), _
    "item_data": CreateMap("seen_globally": True), _
    "is_deleted": False)
    
    Dim map2 As Map = CreateMap("id": "#KK165DFSG8219RR", _
    "type": "COLLECTION", _
    "catalog_data":    CreateMap(), _
    "item_data": CreateMap("seen_globally": True), _
    "is_deleted": False)
    
    Dim list1 As List
    list1.Initialize
    list1.Add(map1)
    list1.Add(map2)
    
    Dim map3 As Map = CreateMap("objects": list1)
    
    Log(map3.As(JSON).ToString)

JSON:
{
    "objects": [
        {
            "is_deleted": false,
            "id": "#56765DFSG8219RR",
            "type": "COLLECTION",
            "catalog_data": {},
            "item_data": {
                "seen_globally": true
            }
        },
        {
            "is_deleted": false,
            "id": "#KK165DFSG8219RR",
            "type": "COLLECTION",
            "catalog_data": {},
            "item_data": {
                "seen_globally": true
            }
        }
    ]
}
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
is there a way to do this programmatically?
Yes.

If you query the resultset from SQLite or other database, you can iterate using NextRow and build your maps inside a few loops.

Just remember, if you want the value look like { } or { "key": value } then you need to create a Map.
For value like [ ], you need a List.

First, make the first level query that should return a list of objects.
Loop through the resultset using Do While resultset.NextRow
Execute another query to get the items for catalog_data (if applicable) and item_data which may be come from another table or join query.

Example:
B4X:
    Dim lstObjects As List
    lstObjects.Initialize
    Dim res1 As ResultSet = SQL.ExecQuery2("SELECT id, type, is_deleted FROM tblProducts WHERE type = ?", Array As String("COLLECTION"))
    Do While res1.NextRow
        Dim id As String = res1.GetString("id")
        Dim strType As String = res1.GetString("type")
        Dim blnDeleted As Boolean
       
        If res1.GetInt("is_deleted") = 1 Then
            blnDeleted = True
        Else
            blnDeleted = False
        End If
       
        ' Get a value for a map using another query
        Dim blnSeen As Boolean
        Dim res2 As ResultSet = SQL.ExecQuery2("SELECT seen_globally FROM tblListings WHERE product_id = ?", Array As String(id))
        Do While res2.NextRow
            If res2.GetInt("seen_globally") = 1 Then
                blnSeen = True
            Else
                blnSeen = False
            End If
        Loop
        res2.Close
       
        Dim Map1 As Map = CreateMap("id": id, _
        "type": strType, _
        "catalog_data": CreateMap(), _
        "item_data": CreateMap("seen_globally": blnSeen), _
        "is_deleted": blnDeleted)
       
        lstObjects.Add(Map1)
    Loop
    res1.Close
   
    Dim MapObjects As Map = CreateMap("objects": lstObjects)
    Log(MapObjects)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…