Page 1 of 1
InsertJSONStructure shouldn't re-sort the structure fields
Posted: Sat Feb 01, 2020 11:08 am
by Sicro
Code: Select all
Structure InfoStruc
ofield1.i
zfield2.i
hfield3.i
EndStructure
Define json
Define info.InfoStruc
json = CreateJSON(#PB_Any)
If json
InsertJSONStructure(JSONValue(json), @info, InfoStruc)
Debug ComposeJSON(json, #PB_JSON_PrettyPrint)
FreeJSON(json)
EndIf
Output is:
Code: Select all
{
"hfield3": 0,
"ofield1": 0,
"zfield2": 0
}
Output should be:
Code: Select all
{
"ofield1": 0,
"zfield2": 0,
"hfield3": 0
}
Re: InsertJSONStructure shouldn't re-sort the structure fiel
Posted: Sat Feb 01, 2020 11:36 am
by Marc56us
I may be wrong, but my understanding is that JSON data is like MAP: it's the system that manages the order of storage whatever structure order.
Try the example of the doc by changing the order: the output will always be the same.
https://www.purebasic.com/documentation ... cture.html
Re: InsertJSONStructure shouldn't re-sort the structure fiel
Posted: Sat Feb 01, 2020 12:17 pm
by Little John
By definition a JSON
object is an
unordered collection of name/value pairs, so this is not a mistake by PureBasic. However, I think PureBasic will be allowed to retain the order.

I would appreciate that, too. So
+1 from me.
I stumbled across the same problem when saving JSON data to a
file, and wrote
my own code to fix that issue.
Re: InsertJSONStructure shouldn't re-sort the structure fiel
Posted: Sat Feb 01, 2020 3:50 pm
by davido
+1
Re: InsertJSONStructure shouldn't re-sort the structure fiel
Posted: Sat Feb 01, 2020 4:39 pm
by NicTheQuick
This is definitely a feature request and no bug at all.
Re: InsertJSONStructure shouldn't re-sort the structure fiel
Posted: Mon Feb 03, 2020 8:04 pm
by Sicro
@Marc56us:
I also realized that the sorting is not random.
@Little John:
Yes, exactly. Whenever the JSON code can be viewed or edited by humans, you want to display the JSON code in a fixed and clear order.
Thanks for your code. I will have a look at it later.
@NicTheQuick:
Right. For me, this is not a bug.
Re: InsertJSONStructure shouldn't re-sort the structure fields
Posted: Thu Mar 14, 2024 12:55 am
by Quin
Is there a solution floating around here that can preserve the order of fields, or at least allow me to re-sort them once I get them from the JSON library?
Re: InsertJSONStructure shouldn't re-sort the structure fields
Posted: Fri Mar 15, 2024 2:56 pm
by spikey
Quin wrote: Thu Mar 14, 2024 12:55 am
Is there a solution floating around here that can preserve the order of fields, or at least allow me to re-sort them once I get them from the JSON library?
InsertJSONStructure and ExtractJSONStructure will "round-trip" properly if used correctly with the same Structure definition. You can ignore the layout in the file - because it's supposed to be order insignificant in the specification. This example still works even though I've changed the input JSON sequence. The order of array members is preserved in the output.
Code: Select all
Structure Person
Name$
Age.l
List Books.s()
EndStructure
Input$ = "{" + Chr(34) + "Age" + Chr(34) + ": 42, " +
Chr(34) + "Books" + Chr(34) + ": [" +
Chr(34) + "Investing For Dummies" + Chr(34) + ", " +
Chr(34) + "A Little Bit of Everything For Dummies" + Chr(34) + "] ," +
Chr(34) + "Name" + Chr(34) + ": " + Chr(34) + "John Smith" + Chr(34) +
"}"
ParseJSON(0, Input$)
ExtractJSONStructure(JSONValue(0), @P.Person, Person)
Debug P\Name$
Debug P\Age
Debug ListSize(P\Books())
ForEach P\Books()
Debug P\Books()
Next