PxPlus User Forum

Twitter Twitter Twitter

Author Topic: JSON from Array with numeric and string  (Read 82 times)

Cedric

  • Silver Member
  • ***
  • Posts: 25
    • View Profile
JSON from Array with numeric and string
« on: March 27, 2024, 12:02:48 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",



Stéphane Devouard

  • Diamond Member
  • *****
  • Posts: 122
  • PxPlus guru with skills in PHP, JS, C#, Java
    • View Profile
    • Stéphane's Web Resume
Re: JSON from Array with numeric and string
« Reply #1 on: March 27, 2024, 12:44:59 PM »
Hi Cedric

Check out the DIM CLASS directive : https://manual.pvxplus.com/PXPLUS/directives/dim_class.htm

Hope this helps
Stéphane Devouard
Portfolio | Work

Loren Doornek

  • Gold Member
  • ****
  • Posts: 85
    • View Profile
Re: JSON from Array with numeric and string
« Reply #2 on: March 27, 2024, 01:37:42 PM »
Thanks for that tip, Stéphane - that's new to me! 

Unfortunately, we have a lot of clients on PXP12, and the DIM CLASS doesn't work on that version since it wasn't added until PxPlus 2016.  I tried it on later versions, and it works well, and is easier to manage than building a string of elements for use with the WITH NUM clause of the DIM.  I typically use the WITH NUM clause, which I described on the other duplicate post in this forum.  But, going forward, I'll probably start using this DIM CLASS instead.

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: JSON from Array with numeric and string
« Reply #3 on: March 28, 2024, 12:28:19 PM »
Another simple solution, which will work when using older PxPlus, is to predefine the JSON structure then load the values:

For example, we can preload the numeric JSON elements with zero values.  Only the numeric values need be declared.

->x$="{'Balance':0}"
->dim load json$=x$


The above will create the array json$ with add the element Balance marked as numeric.  Now you can define the various values you need.

->json$["Name"]="Mike King"
->json$["Addr"]="123 Main St"
->json$["Balance"]="123.45"
->json$["Email"]="mike.king@bbsysco.com"


When the Array is converted back to JSON the Numeric indicator for Balance is remembered.

->print dim(list edit json$)
{
  "Addr":"123 Main St",
  "Balance":123.45,
  "Email":"mike.king@bbsysco.com",
  "Name":"Mike King"
}




« Last Edit: March 28, 2024, 02:58:32 PM by Mike King »
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com