B4J Question How to generate json string

Chris Guanzon

Active Member
Licensed User
Longtime User
Hello everyone,

Please help me on how to generate this kind of json format?

B4X:
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Thank in advance. ☺️
 

Chris Guanzon

Active Member
Licensed User
Longtime User
Show your original data

This is what I can do so far.

B4X:
Dim data As List
data.Initialize
data.Clear
For xx = 0 To 10' Contactz.Size-1
    data.Add(CreateMap("name": xx, "mobile": xx))
Next

JSONGenerator.Initialize2(data)
Log (JSONGenerator.ToPrettyString(1))
resp.Write(JSONGenerator.ToPrettyString(1))

and this is the output

B4X:
[
    {
        "name": 0,
        "mobile": 0
    },
    {
        "name": 1,
        "mobile": 1
    },
    {
        "name": 2,
        "mobile": 2
    },
    {
        "name": 3,
        "mobile": 3
    },
    {
        "name": 4,
        "mobile": 4
    },
    {
        "name": 5,
        "mobile": 5
    },
    {
        "name": 6,
        "mobile": 6
    },
    {
        "name": 7,
        "mobile": 7
    },
    {
        "name": 8,
        "mobile": 8
    },
    {
        "name": 9,
        "mobile": 9
    },
    {
        "name": 10,
        "mobile": 10
    }
]
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
simple test
B4X:
    Dim menuitem As List
    menuitem.Initialize
    menuitem.Add(CreateMap("value": "New", "onclick": "CreateNewDoc()"))
    menuitem.Add(CreateMap("value": "Open", "onclick": "OpenDoc()"))
    menuitem.Add(CreateMap("value": "Close", "onclick": "CloseDoc()"))
    Dim mRoot As Map = CreateMap("menu": CreateMap("id": "file", "value": "File", "popup" : CreateMap("menuitem": menuitem)))
    Log(mRoot.As(JSON).ToString)
JSON:
{
    "menu": {
        "popup": {
            "menuitem": [
                {
                    "onclick": "CreateNewDoc()",
                    "value": "New"
                },
                {
                    "onclick": "OpenDoc()",
                    "value": "Open"
                },
                {
                    "onclick": "CloseDoc()",
                    "value": "Close"
                }
            ]
        },
        "id": "file",
        "value": "File"
    }
}
 
Upvote 1

Chris Guanzon

Active Member
Licensed User
Longtime User
simple test
B4X:
    Dim menuitem As List
    menuitem.Initialize
    menuitem.Add(CreateMap("value": "New", "onclick": "CreateNewDoc()"))
    menuitem.Add(CreateMap("value": "Open", "onclick": "OpenDoc()"))
    menuitem.Add(CreateMap("value": "Close", "onclick": "CloseDoc()"))
    Dim mRoot As Map = CreateMap("menu": CreateMap("id": "file", "value": "File", "popup" : CreateMap("menuitem": menuitem)))
    Log(mRoot.As(JSON).ToString)
JSON:
{
    "menu": {
        "popup": {
            "menuitem": [
                {
                    "onclick": "CreateNewDoc()",
                    "value": "New"
                },
                {
                    "onclick": "OpenDoc()",
                    "value": "Open"
                },
                {
                    "onclick": "CloseDoc()",
                    "value": "Close"
                }
            ]
        },
        "id": "file",
        "value": "File"
    }
}

Thank you very much @oparra.
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
Hi @oparra,

Can I ask for help with this? This is what I want the json output


B4X:
{
    "message": "The data you provided is invalid.",
    "error": [
        {
            "platform": "platform is required."
        },
        {
            "version": "version is required."
        }
    ],
    "status": 422
}

here's my code


B4X:
Dim paramsList As List
paramsList.Initialize
For Each paramsValue As String In Array(platform, version)
    If paramsValue = "" Then
        paramsList.Add(CreateMap(paramsValue : $"${paramsValue} is required."$))
    End If
Next

If paramsList.Size <> 0 Then
    Dim error As Map = CreateMap("status":422,"message":"The data you provided is invalid.","error": paramsList)
    JSONGenerator.Initialize(error)
    resp.Write(JSONGenerator.ToPrettyString(1))
end if

and this is the output

B4X:
{
    "message": "The data you provided is invalid.",
    "error": [
        {
            "": " is required."
        },
        {
            "": " is required."
        }
    ],
    "status": 422
}

How can I get the "platform" and "version" and add it in json output so I can achieve the output from the top json output?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
    Dim ParamsList As List
    ParamsList.Initialize
    
    For Each paramsValue As String In Array("version", "platform")
        Dim m As Map = CreateMap(paramsValue : $"${paramsValue} is required."$)
        ParamsList.Add(m)
    Next

    Dim Error As Map = CreateMap("status" : 422, "message" : "The data you provided is invalid.", "error": ParamsList)
    Log(Error.As(JSON).ToString)

1643721405920.png
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
?
B4X:
    Dim ParamsList As List
    ParamsList.Initialize
   
    For Each paramsValue As String In Array("version", "platform")
        Dim m As Map = CreateMap(paramsValue : $"${paramsValue} is required."$)
        ParamsList.Add(m)
    Next

    Dim Error As Map = CreateMap("status" : 422, "message" : "The data you provided is invalid.", "error": ParamsList)
    Log(Error.As(JSON).ToString)

View attachment 125002


Thank you so much! I really appreciate your help.
 
Upvote 0
Top