B4J Question JSONTOOL - Parsing Json to retrieve some information

mcqueccu

Well-Known Member
Licensed User
Longtime User
I am using the online json tool to parse this json, but it seems the json is having repeated keys hence the json tool is unable to give me the correct code. Does anyone know how to get this informations from this json.

Here are the informations I want to get from the json,

XXXX_USERID_HERE
XXXX_BASE64_IMAGE_HERE
XXXX_STATUS_HERE
XXXX_LASTNAME_HERE
XXXX_OTHERNAMES_HERE
XXXX_DOB_HERE
XXXX_POLLINGSTATION_HERE
XXXX_CONSTITUENCY_HERE
XXXX_DISTRICT_HERE
XXXX_REGION_HERE
XXXX_REGISTRATIONDATE_HERE

Code from json tool:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim input As Map = jRoot.Get("input")
Dim id As String = input.Get("id")
Dim type As String = input.Get("type")
Dim items As List = input.Get("items")
For Each colitems As Map In items
 Dim id As String = colitems.Get("id")
 Dim content As String = colitems.Get("content")
 Dim outgoingEdgeId As String = colitems.Get("outgoingEdgeId")
Next
Dim messages As List = jRoot.Get("messages")
For Each colmessages As Map In messages
 Dim id As String = colmessages.Get("id")
 Dim type As String = colmessages.Get("type")
 Dim content As Map = colmessages.Get("content")
 Dim type As String = content.Get("type")
 Dim richText As List = content.Get("richText")
 For Each colrichText As Map In richText
  Dim children As List = colrichText.Get("children")
  For Each colchildren As Map In children
   Dim text As String = colchildren.Get("text")
  Next
  Dim type As String = colrichText.Get("type")
 Next
Next
Dim logs As List = jRoot.Get("logs")



JSON String:
{
    "messages": [{
            "id": "lib14rs0pew3468a3gyvdeha",
            "type": "text",
            "content": {
                "type": "richText",
                "richText": [{
                        "type": "p",
                        "children": [{
                                "text": "Searching for "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_USERID_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }, {
                                "text": ". Please wait..."
                            }
                        ]
                    }
                ]
            }
        }, {
            "id": "hxcwy72ycv7capmk5glm6b9y",
            "type": "image",
            "content": {
                "url": "data:image/png;base64,XXXX_BASE64_IMAGE_HERE"
            }
        }, {
            "id": "qqurgm4zoa7o6n3lxpdrfbv6",
            "type": "text",
            "content": {
                "type": "richText",
                "richText": [{
                        "type": "p",
                        "children": [{
                                "text": "Status: "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_STATUS_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }, {
            "id": "bajvdw2ueqzontkgz4oomnzc",
            "type": "text",
            "content": {
                "type": "richText",
                "richText": [{
                        "type": "p",
                        "children": [{
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_LASTNAME_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }, {
                                "bold": true,
                                "text": " "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_OTHERNAMES_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": "Date of Birth:"
                            }, {
                                "bold": true,
                                "text": " "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_DOB_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": "Polling Station: "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_POLLINGSTATION_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }, {
            "id": "qcjcm194qx8o19dw0koyuwox",
            "type": "text",
            "content": {
                "type": "richText",
                "richText": [{
                        "type": "p",
                        "children": [{
                                "text": "Constituency: "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_CONSTITUENCY_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": "District: "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_DISTRICT_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": "Region:"
                            }, {
                                "bold": true,
                                "text": " "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_REGION_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": ""
                            }
                        ]
                    }, {
                        "type": "p",
                        "children": [{
                                "text": "Date of Registration: "
                            }, {
                                "type": "inline-variable",
                                "children": [{
                                        "type": "p",
                                        "children": [{
                                                "bold": true,
                                                "text": "XXXX_REGISTRATIONDATE_HERE"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }
    ],
    "input": {
        "id": "agfeet8x30smlyr88tk16zi8",
        "type": "choice input",
        "items": [{
                "id": "iysj65jb8kv93ygi5fty9kur",
                "outgoingEdgeId": "bkl6eibkhvojqce4anxpsoiw",
                "content": "Check Another"
            }
        ]
    },
    "logs": []
}
 
Solution
The previous message should have been the answer... BUT it wasn't.
After doing some testing here's the code i wrote to get to the texts:
B4X:
    Dim jstr As String = File.ReadString(File.DirAssets,"mcqueccu.json")
    jmap = jstr.As(JSON).ToMap
'    Dim input As Map = jmap.Get("input")
'    Dim inputid As String = input.Get("id")
'    Dim inputtype As String = input.Get("type")
'    Dim inputitems As List = input.Get("items")
'    For Each colinputitems As Map In inputitems
'        Log(colinputitems)
'        Dim colinputid As String = colinputitems.Get("id")
'        Dim colinputcontent As String = colinputitems.Get("content")
'        Dim colinputoutgoingEdgeId As String = colinputitems.Get("outgoingEdgeId")
'    Next
    Dim messages...

PaulMeuris

Active Member
Licensed User
With this tool from @Erel JsontTree you can get this result:
1729481337755.png

Showing the text variable content should do the trick...
 
Upvote 0

PaulMeuris

Active Member
Licensed User
The previous message should have been the answer... BUT it wasn't.
After doing some testing here's the code i wrote to get to the texts:
B4X:
    Dim jstr As String = File.ReadString(File.DirAssets,"mcqueccu.json")
    jmap = jstr.As(JSON).ToMap
'    Dim input As Map = jmap.Get("input")
'    Dim inputid As String = input.Get("id")
'    Dim inputtype As String = input.Get("type")
'    Dim inputitems As List = input.Get("items")
'    For Each colinputitems As Map In inputitems
'        Log(colinputitems)
'        Dim colinputid As String = colinputitems.Get("id")
'        Dim colinputcontent As String = colinputitems.Get("content")
'        Dim colinputoutgoingEdgeId As String = colinputitems.Get("outgoingEdgeId")
'    Next
    Dim messages As List = jmap.Get("messages")
    For i = 0 To messages.Size -1
        Dim message As Map = messages.Get(i)
'        Log(message)
        Dim msgcontent As Map = message.Get("content")
'        Log(msgcontent)
        Dim richtextlst As List = msgcontent.Get("richText")
'        Log(richtextlst)
        If richtextlst.IsInitialized Then
            For j = 0 To richtextlst.Size -1
                If richtextlst.Get(j) <> Null Then
                    Dim childrenmap As Map = richtextlst.Get(j)
'                    Log(childrenmap)
                    Dim grandchildrenlst As List = childrenmap.Get("children")
'                    Log(grandchildrenlst)
                    For k = 0 To grandchildrenlst.Size -1
                        Dim grandchildmap As Map = grandchildrenlst.Get(k)
'                        Log(grandchildmap)
                        Dim overgrandchildlst As List = grandchildmap.Get("children")
'                        Log(overgrandchildlst)
                        If overgrandchildlst.IsInitialized Then
                            For m = 0 To overgrandchildlst.Size -1
                                Dim overgrandchildmap As Map = overgrandchildlst.Get(m)
                                Dim lastlst As List = overgrandchildmap.Get("children")
'                                Log(lastlst)
                                If lastlst.IsInitialized Then
                                    For n = 0 To lastlst.Size -1
                                        Dim lastmap As Map = lastlst.Get(n)
'                                        Log(lastmap)
                                        If lastmap.ContainsKey("text") And lastmap.Get("text") <> Null Then
                                            Log(lastmap.Get("text"))
                                        End If
                                    Next
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        End If
        If msgcontent.ContainsKey("url") Then
            Dim msgurl As String = msgcontent.Get("url")
            Log(msgurl.SubString(msgurl.LastIndexOf(",")+1))
        End If
    Next
And this is the result in the log:
Waiting for debugger to connect...
Program started.
*** mainpage: B4XPage_Created
XXXX_USERID_HERE
XXXX_BASE64_IMAGE_HERE
XXXX_STATUS_HERE
XXXX_LASTNAME_HERE
XXXX_OTHERNAMES_HERE
XXXX_DOB_HERE
XXXX_POLLINGSTATION_HERE
XXXX_CONSTITUENCY_HERE
XXXX_DISTRICT_HERE
XXXX_REGION_HERE
XXXX_REGISTRATIONDATE_HERE
*** mainpage: B4XPage_Appear
*** mainpage: B4XPage_Resize [mainpage]
*** mainpage: B4XPage_Disappear [mainpage]
You can uncomment some lines to see the contents of the different maps and lists.
You can find the source code in the attachment (testenvironment74.zip)
NOTE: this solution only works for the given JSON structure.
 

Attachments

  • testenvironment74.zip
    4 KB · Views: 14
Upvote 0
Solution

mcqueccu

Well-Known Member
Licensed User
Longtime User
The previous message should have been the answer... BUT it wasn't.
After doing some testing here's the code i wrote to get to the texts:
B4X:
    Dim jstr As String = File.ReadString(File.DirAssets,"mcqueccu.json")
    jmap = jstr.As(JSON).ToMap
'    Dim input As Map = jmap.Get("input")
'    Dim inputid As String = input.Get("id")
'    Dim inputtype As String = input.Get("type")
'    Dim inputitems As List = input.Get("items")
'    For Each colinputitems As Map In inputitems
'        Log(colinputitems)
'        Dim colinputid As String = colinputitems.Get("id")
'        Dim colinputcontent As String = colinputitems.Get("content")
'        Dim colinputoutgoingEdgeId As String = colinputitems.Get("outgoingEdgeId")
'    Next
    Dim messages As List = jmap.Get("messages")
    For i = 0 To messages.Size -1
        Dim message As Map = messages.Get(i)
'        Log(message)
        Dim msgcontent As Map = message.Get("content")
'        Log(msgcontent)
        Dim richtextlst As List = msgcontent.Get("richText")
'        Log(richtextlst)
        If richtextlst.IsInitialized Then
            For j = 0 To richtextlst.Size -1
                If richtextlst.Get(j) <> Null Then
                    Dim childrenmap As Map = richtextlst.Get(j)
'                    Log(childrenmap)
                    Dim grandchildrenlst As List = childrenmap.Get("children")
'                    Log(grandchildrenlst)
                    For k = 0 To grandchildrenlst.Size -1
                        Dim grandchildmap As Map = grandchildrenlst.Get(k)
'                        Log(grandchildmap)
                        Dim overgrandchildlst As List = grandchildmap.Get("children")
'                        Log(overgrandchildlst)
                        If overgrandchildlst.IsInitialized Then
                            For m = 0 To overgrandchildlst.Size -1
                                Dim overgrandchildmap As Map = overgrandchildlst.Get(m)
                                Dim lastlst As List = overgrandchildmap.Get("children")
'                                Log(lastlst)
                                If lastlst.IsInitialized Then
                                    For n = 0 To lastlst.Size -1
                                        Dim lastmap As Map = lastlst.Get(n)
'                                        Log(lastmap)
                                        If lastmap.ContainsKey("text") And lastmap.Get("text") <> Null Then
                                            Log(lastmap.Get("text"))
                                        End If
                                    Next
                                End If
                            Next
                        End If
                    Next
                End If
            Next
        End If
        If msgcontent.ContainsKey("url") Then
            Dim msgurl As String = msgcontent.Get("url")
            Log(msgurl.SubString(msgurl.LastIndexOf(",")+1))
        End If
    Next
And this is the result in the log:

You can uncomment some lines to see the contents of the different maps and lists.
You can find the source code in the attachment (testenvironment74.zip)
NOTE: this solution only works for the given JSON structure.
Wow. Thank you very much. This is excellent work.


I use the Json tree tool a lot but it seems this nested tool, is too complicated for it based on the repeated names, and levels.
I think the JsonTree Tool needs to be reviewed, to be able to detect and assign different variable names based on level of the tree (parents, grand children, etc) just like the way you did.
 
Upvote 0
Top