PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Json element class  (Read 1127 times)

michaelgreer

  • Diamond Member
  • *****
  • Posts: 129
    • View Profile
Json element class
« 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?

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 element class
« Reply #1 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
Stéphane Devouard
Portfolio | Work

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Json element class
« Reply #2 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)



Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

michaelgreer

  • Diamond Member
  • *****
  • Posts: 129
    • View Profile
Re: Json element class
« Reply #3 on: July 12, 2022, 11:57:53 AM »
This will work. Thanks.  I had considered it but couldn't find the exact syntax described.  ::)