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.
@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.
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?
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.