B4J Question How to keep proper JSON order?

techknight

Well-Known Member
Licensed User
Longtime User
I think I ran into this one in the past but I don't recall.

I am trying to figure out a way to keep proper JSON output order, like so:

{"ClockMinutes":15,"ClockSeconds":0,"ClockMilliseconds":0,"HomeScore":0,"GuestScore":0,"Inning":1,"Ball":0,"Strike":0,"Out":0}

My routine looks like this:
B4X:
'Sends all scoreboard data to the HMI over the ScoreData topic. 
Sub RefreshDataToHMI(Targetaddress As Int)
    Dim Response As Map
    Dim JSON As JSONGenerator
    Response.Initialize
    'Put Scoreboard scorekeeping data into JSON Map
    Dim ScoreData As Map = Main.Scorekeepers(Targetaddress).GetScoreData
    Dim Baseball As BaseballScoreData = ScoreData.Get("Baseball")
    Dim GameClock As GameClockData = ScoreData.Get("GameClock")
    
    Response.Put("ClockMinutes", GameClock.Minutes)
    Response.Put("ClockSeconds", GameClock.Seconds)
    Response.Put("ClockMilliseconds", GameClock.Milliseconds)
    Response.Put("HomeScore", Baseball.Home)
    Response.Put("GuestScore", Baseball.Guest)
    Response.Put("Inning", Baseball.Inning)
    Response.Put("Ball", Baseball.Ball)
    Response.Put("Strike", Baseball.Strike)
    Response.Put("Out", Baseball.Out)
    JSON.Initialize(Response)                                                                            'Build the JSON Object
    Dim JSONResponse As String = JSON.ToString                                                            'Save it into a string
    If mqtt.Connected = True Then mqtt.Publish("ScoreData/" & Targetaddress, JSONResponse.GetBytes("UTF-8"))    'Send JSON String to HMI
End Sub

But it does not maintain the same order as the above JSON which causes me issues down the line.

any thoughts on how to fix this? thanks
 

techknight

Well-Known Member
Licensed User
Longtime User
Key/value stores, generally, do not preserve the order (B4J/A Map and B4XOrderedMap do preserve the order).
You cannot make the JSON library preserve order of Map keys.

Why is the order important?
Why aren't you using B4XSerializator (it will preserve order)?

Because the program which receives this data on the other end is not a B4X program. its a Node.JS program so i need to preserve the order somehow.

(This program is also being written by a different developer so I have not seen/know his code.)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Maps are accessed by their keys. It is very strange that the order matters. This is true to any programming language.

You will need to build the json string yourself. It doesn't look too complicated in this case.

I suppose itll all determine how hes parsing JSON on his side then i guess. I dont have any experience with JS or Node, so I dont know how that works.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
For whatever it's worth, it's fair to consider his code broken if he expects a certain order in the json.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
For whatever it's worth, it's fair to consider his code broken if he expects a certain order in the json.
Yeah, I figured. I just wanted to cover my bases.

In theory, the vibe I am getting here is order should not matter regardless.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
A dirty trick for your JSON could be to output a List of maps.
That should keep the order.
I'm not advocating it as a nice solution, but if it can help to satisfy your programming partner..
 
Upvote 0
Top