B4J Question Many different json files for different table in DB

yfleury

Active Member
Licensed User
Longtime User
For each json files (stucture is different) I have to create a table in database. What I want, is to show a list of each element (in the list or map, without value) to create table and colums
For a json code like this
B4X:
{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    [
                        399871.32121978863,
                        5418344.341168709
                    ],
                    [
                        399768.0000036924,
                        5418367.00000073
                    ],
                    [
                        399745.9999876719,
                        5418357.000000721
                    ],
                    [
                        399661.5641475932,
                        5418293.268160662
                    ]
                ],
                "type": "LineString"
            },
            "type": "Feature",
            "properties": {
                "SECTEUR": "Mistassini",
                "Matériau": "Fonte ductile",
                "Troncon": 20061,
                "LENGTH": 129.942726271,
                "INSTALLATI": 1990,
                "OBJECTID": 1,
                "Shape_Leng": 235.731013,
                "CODE": "C-3924",
                "OWNER": "Ville",
                "Type_de_co": "D",
                "Hiérarchi": "III",
                "SECTION": "A-20061",
                "Diamètre": "200"
            }
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        400395.66565227695,
                        5415587.208606142
                    ],
                    [
                        400351.8097322361,
                        5415645.336254195
                    ]
                ],
                "type": "LineString"
            },
            "type": "Feature",
            "properties": {
                "SECTEUR": "Mistassini",
                "Matériau": "Fonte ductile",
                "Troncon": 20216,
                "LENGTH": 115.279953663,
                "INSTALLATI": 1992,
                "OBJECTID": 3,
                "Shape_Leng": 72.8159679664,
                "CODE":

What I need is
features
geometry
coordinates
type
properties
SECTEUR
Matériau
Troncon
etc...
to build table with these names for colums

I know the json tree tool run wery well. But I don't want to use each time for each json file. After table was created, I can fill it with data
 

teddybear

Well-Known Member
Licensed User
For each json files (stucture is different) I have to create a table in database. What I want, is to show a list of each element (in the list or map, without value) to create table and colums
For a json code like this
B4X:
{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    [
                        399871.32121978863,
                        5418344.341168709
                    ],
                    [
                        399768.0000036924,
                        5418367.00000073
                    ],
                    [
                        399745.9999876719,
                        5418357.000000721
                    ],
                    [
                        399661.5641475932,
                        5418293.268160662
                    ]
                ],
                "type": "LineString"
            },
            "type": "Feature",
            "properties": {
                "SECTEUR": "Mistassini",
                "Matériau": "Fonte ductile",
                "Troncon": 20061,
                "LENGTH": 129.942726271,
                "INSTALLATI": 1990,
                "OBJECTID": 1,
                "Shape_Leng": 235.731013,
                "CODE": "C-3924",
                "OWNER": "Ville",
                "Type_de_co": "D",
                "Hiérarchi": "III",
                "SECTION": "A-20061",
                "Diamètre": "200"
            }
        },
        {
            "geometry": {
                "coordinates": [
                    [
                        400395.66565227695,
                        5415587.208606142
                    ],
                    [
                        400351.8097322361,
                        5415645.336254195
                    ]
                ],
                "type": "LineString"
            },
            "type": "Feature",
            "properties": {
                "SECTEUR": "Mistassini",
                "Matériau": "Fonte ductile",
                "Troncon": 20216,
                "LENGTH": 115.279953663,
                "INSTALLATI": 1992,
                "OBJECTID": 3,
                "Shape_Leng": 72.8159679664,
                "CODE":

What I need is
features
geometry
coordinates
type
properties
SECTEUR
Matériau
Troncon
etc...
to build table with these names for colums

I know the json tree tool run wery well. But I don't want to use each time for each json file. After table was created, I can fill it with data
Using Regex extracts them
B4X:
data = $"{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    [
                        399871.32121978863,
                        5418344.341168709
                    ],
        ......
                        "INSTALLATI": 1992,
                "OBJECTID": 3,
                "Shape_Leng" : 72.8159679664,
                "CODE" :"$
    data = data.Replace(",", ","&CRLF) 
    data = data.Replace(":", ":"&CRLF)   
    data = data.Replace(" ", "")   
    Dim matcher1 As Matcher
    Dim fields as list 'This is what you need
    fields.Initialize
    matcher1 = Regex.Matcher($""(.*)":"$, data)
    Do While matcher1.Find = True
        Dim fld() As String
        fld = Regex.Split( $"""$, matcher1.Match)
        fields.add(fld(1))
    Loop
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
@teddybear your code works well, but all names come many times I need one time without copy
Just changes fields type list to map
Code as below
B4X:
    Dim matcher1 As Matcher
    Dim fields As Map
    fields.Initialize
    matcher1 = Regex.Matcher($""(.*)":"$, data)
    Do While matcher1.Find = True
        Dim fld() As String
        fld = Regex.Split( $"""$, matcher1.Match)
        fields.put(fld(1),1)
    Loop
    For Each j As String In fields.Keys
        Log(j) 'This is what you need
    Next
 
Upvote 0
Top