PxPlus User Forum

Main Board => Discussions => Language => Topic started by: michaelgreer on July 12, 2022, 10:39:31 AM

Title: Json element class
Post by: michaelgreer on July 12, 2022, 10:39:31 AM
I am looping through a PO to build a json like this
po$["po.order_items.1.item"]="widget1"
po$["po.order_items.1.price"]="10.25"
po$["po.order_items.1.qty"]="2"

There could be anywhere between 1 and several hundred elements in the items array.  Oddly, I need to output price as a numeric ( but not quantity). Is there a way to do this without have to specify *every* price element in the "with num()" phrase?
Title: Re: Json element class
Post by: Stéphane Devouard on July 12, 2022, 10:58:13 AM
Michael

Unless there are some undocument features in PxPlus JSON support via associative array, I have bad news : you must provide all the array keys that have numeric values in the with num() clause

PxPlus JSON support is great but it lacks some helper functions, especially in the area of JSON arrays
Something like DIM(READ NUM(json$["po.order_items."]) would be nice to count the number of elements in the order_items[] array
As well as being able to specify something like "po.order_items.{*}.price" in the with num() clause

Regards
Title: Re: Json element class
Post by: Mike King on July 12, 2022, 11:44:59 AM
Have you considered using the DIM CLASS directive as in:

po$["po.order_items.1.item"]="widget1"
po$["po.order_items.1.price"]="10.25"
dim class po$["po.order_items.1.price"]="N"
po$["po.order_items.1.qty"]="2"
dim class po$["po.order_items.1.qty"]="N"


Or perhaps make a simple function of it:

DEF FN_poSetNum(local idx$, local val)
 po$[idx$]=str(val)
 dim class po$[idx$]="N"
 return
End Def


Then

po$["po.order_items.1.item"]="widget1"
fn_poSetNum("po.order_items.1.price", 10.25)
fn_poSetNum("po.order_items.1.qty", 2)



Title: Re: Json element class
Post by: michaelgreer on July 12, 2022, 11:57:53 AM
This will work. Thanks.  I had considered it but couldn't find the exact syntax described.  ::)