PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: Cedric on March 27, 2024, 12:12:59 PM

Title: JSON from Array with numeric and string
Post by: Cedric on March 27, 2024, 12:12:59 PM
Hi,

I'm wondering if there was a way I could "force" a json value to be a numeric when the Array is set to string.   I have a case where I need to send the data in a specific format for the end user (not pvx) who uses variable types.

So my data is all sent as if they were string, but I'd like one specific value to be without quotation mark, so as a numeric value.

I thought that maybe there was a prefix I could put before the value so that PVX would recognize it as a numeric for the end result of :

"value": 1,

instead of

"value": "1",


Title: Re: JSON from Array with numeric and string
Post by: Loren Doornek on March 27, 2024, 01:16:32 PM
Use the "with num" option for JSON to specify which values are numeric.  Below is an example.

arr$["string"]="123"
arr$["numeric"]="123"
print dim(list edit arr${all} with num("numeric"))

{
  "string":"123",
  "numeric":123
}
Title: Re: JSON from Array with numeric and string
Post by: Loren Doornek on March 27, 2024, 01:24:42 PM
Here is a better example, which uses a comma-delimited list of the JSON elements that should be numeric.

0010 BEGIN
0020 LET arr$["string"]="123"
0030 LET arr$["numeric"]="123",withnum$+="numeric,"
0040 LET arr$["this.is.a.string"]="456"
0050 LET arr$["this.is.a.number"]="456",withnum$+="this.is.a.number,"
0060 PRINT "Withnum$= ",withnum$
0070 PRINT DIM(LIST EDIT arr${ALL} WITH NUM(withnum$))

run

Withnum$= numeric,this.is.a.number,
{
  "string":"123",
  "numeric":123,
  "this":{
    "is":{
      "a":{
        "string":"456",
        "number":456
      }
    }
  }
}