json bool

Just starting out? Need help? Post your questions and find answers here.
Garfield
New User
New User
Posts: 8
Joined: Sun Dec 10, 2023 6:32 pm

json bool

Post by Garfield »

The following simple example:

Code: Select all

Structure json_
  test1.b
  test2.b
EndStructure

Define json.json_

json\test1 = #True
json\test2 = #False

js = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(js), @json, json_)
Debug ComposeJSON(js, #PB_JSON_PrettyPrint)
the output should be:
{
"test1": true,
"test2": false
}
but it is
{
"test2": 0,
"test1": 1
}
My two questions:

Why is bool interpreted wrong? In Json 0 and 1 are numbers and not booleans.

Why is the output not in the order of how it was declared the structure?
User avatar
mk-soft
Always Here
Always Here
Posts: 6261
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: json bool

Post by mk-soft »

With PureBasic, the variable type Boolean does not exist.

Variable var.b is a signed byte ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Garfield
New User
New User
Posts: 8
Joined: Sun Dec 10, 2023 6:32 pm

Re: json bool

Post by Garfield »

Yes, but byte is no data type in Json. (only string, number, bool, array and object)

So I thought, it would be interpreted as boolean.

Is there a good way to get the right output from insert/compose?

Or do I have to do a dirty way with defining it as string in structure and do a replacement after compose?
Little John
Addict
Addict
Posts: 4793
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: json bool

Post by Little John »

@Garfield

You can use SetJSONBoolean().

As far as the order is concerned:
InsertJSONStructure() creates a JSON object, and the order of members of a JSON object is not defined. You can use Save JSON data with object members well-arranged to set their order.
User avatar
HeX0R
Addict
Addict
Posts: 1206
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: json bool

Post by HeX0R »

I came accross that also in the past, it removes the ability to easily extract/insert structures, when boolean values are needed (for external APIs e.g.).
Would be cool, if we had some flag for extract/insertstructure, like #PB_JSON_BYTES_EQUAL_BOOLEAN or similar
Little John
Addict
Addict
Posts: 4793
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: json bool

Post by Little John »

HeX0R wrote: Tue Mar 18, 2025 6:19 pm Would be cool, if we had some flag for extract/insertstructure, like #PB_JSON_BYTES_EQUAL_BOOLEAN or similar
+1
Post Reply