Re: TO JSON with dynamic array
Iwan AB <[email protected]> Tue, 30 Sep 2025 13:36:21 +0700
| Newsgroups | gmane.comp.lang.4gl.aubit.general |
|---|---|
| Message-ID | <CAHJCSmLbW2i_t3xHC+juEtZ4Y8-yCYKYhT0vqYLQnd6V5VK+0w@mail.gmail.com> |
--===============7512444516078865788== Content-Type: multipart/alternative; boundary="00000000000029ea05063ffef92a" --00000000000029ea05063ffef92a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Oops, my mistake. Thanks for pointing that out. On Tue, Sep 30, 2025, 1:29=E2=80=AFPM Mike Aubury <[email protected]> wrote: > You're using it wrong - you need to use : > > > > To deserialize JSON use - LET FROM JSON > > To serialize JSON - use TO JSON(ws) > > MAIN > DEFINE ws CHAR(200) > DEFINE wsNew CHAR(200) > DEFINE wr RECORD > data ARRAY[5] OF CHAR(1) > END RECORD > > LET ws =3D '{ "data": [ "a", "b", "c", "d", "e" ] }' > > > # Deserialise > LET FROM JSON wr =3D ws > > DISPLAY "1=3D", wr.data[1] > DISPLAY "2=3D", wr.data[2] > DISPLAY "3=3D", wr.data[3] > DISPLAY "4=3D", wr.data[4] > DISPLAY "5=3D", wr.data[5] > > # Serialise > LET wsNew=3DTO JSON (wr) > > DISPLAY wsNew clipped > > END MAIN > > > So your line > LET FROM JSON wr.data =3D TO JSON(ws) > > Was serialising the string (which leaves you with a JSON string), then > trying to deserialize the string into the array (which obviously > wouldnt work) > > > On Tue, 30 Sept 2025 at 01:43, Iwan AB <[email protected]> wrote: > >> Great. Thank you very much. >> Btw, I've test the following code: >> >> MAIN >> DEFINE ws CHAR(200) >> DEFINE wr RECORD >> data ARRAY[5] OF CHAR(1) >> END RECORD >> >> LET ws =3D '{ "data": [ "a", "b", "c", "d", "e" ] }' >> LET FROM JSON wr.data =3D TO JSON(ws) >> >> DISPLAY "1=3D", wr.data[1] >> DISPLAY "2=3D", wr.data[2] >> DISPLAY "3=3D", wr.data[3] >> DISPLAY "4=3D", wr.data[4] >> DISPLAY "5=3D", wr.data[5] >> >> END MAIN >> >> the output is: >> 1=3D >> 2=3D >> 3=3D >> 4=3D >> 5=3D >> >> instead of: >> 1=3Da >> 2=3Db >> 3=3Dc >> 4=3Dd >> 5=3De >> >> >> On Tue, Sep 30, 2025 at 12:38=E2=80=AFAM Mike Aubury <[email protected]> wr= ote: >> >>> Thats fixed in SVN now >>> >>> $ cat prog.4gl >>> MAIN >>> DEFINE wa DYNAMIC ARRAY OF RECORD >>> id CHAR(2), >>> product CHAR(60) >>> END RECORD >>> DEFINE i, n SMALLINT >>> DEFINE w_json STRING >>> >>> ALLOCATE ARRAY wa[10] >>> >>> >>> LET wa[1].id=3D1 >>> LET wa[1].product=3D"Product 1" >>> LET wa[2].id=3D2 >>> LET wa[2].product=3D"Product 2" >>> LET wa[3].id=3D3 >>> LET wa[3].product=3D"Product 3" >>> LET wa[4].id=3D4 >>> LET wa[4].product=3D"Product 4" >>> >>> display wa[1].id >>> display wa[2].id >>> display wa[3].id >>> display wa[4].id >>> >>> RESIZE ARRAY wa[4] >>> >>> LET w_json =3D TO JSON(wa) >>> DISPLAY w_json >>> >>> END MAIN >>> >>> $ 4glpc -g prog.4gl -o prog.4ae >>> $ ./prog.4ae >>> 1 >>> 2 >>> 3 >>> 4 >>> [{"id":"1","product":"Product 1"},{"id":"2","product":"Product >>> 2"},{"id":"3","product":"Product 3"},{"id":"4","product":"Product 4"}] >>> >>> On Mon, 29 Sept 2025 at 18:04, Iwan AB <[email protected]> wrote: >>> >>>> Dear Mike, I've tried the JSON operation. It is awesome, thank you. Bu= t >>>> if I may suggest, it would be nice, if it could be implemented with DY= NAMIC >>>> ARRAY also. >>>> >>>> with static array: >>>> MAIN >>>> DEFINE wa ARRAY[5] OF RECORD >>>> id CHAR(2), >>>> product CHAR(60) >>>> END RECORD >>>> DEFINE i, n SMALLINT >>>> DEFINE w_json STRING >>>> >>>> DECLARE cur1 CURSOR FOR >>>> SELECT id, product FROM t1 >>>> >>>> LET i =3D 1 >>>> FOREACH cur1 INTO wa[i].* >>>> LET i =3D i + 1 >>>> END FOREACH >>>> >>>> LET w_json =3D TO JSON(wa) >>>> DISPLAY w_json >>>> >>>> END MAIN >>>> >>>> OUTPUT: >>>> [{"id": "1", "product": "P01"},{"id": "2", "product": "P02"},{"id": >>>> "3", "product": "P03"}, {"id": "", "product": ""}, {"id":"", "product"= : >>>> ""}] >>>> >>>> with dynamic array: >>>> MAIN >>>> DEFINE wa DYNAMIC ARRAY OF RECORD >>>> id CHAR(2), >>>> product CHAR(60) >>>> END RECORD >>>> DEFINE i, n SMALLINT >>>> DEFINE w_json STRING >>>> >>>> ALLOCATE ARRAY wa[10] >>>> >>>> DECLARE cur1 CURSOR FOR >>>> SELECT id, product FROM t1 >>>> >>>> LET i =3D 1 >>>> FOREACH cur1 INTO wa[i].* >>>> LET i =3D i + 1 >>>> END FOREACH >>>> LET n =3D i - 1 >>>> >>>> RESIZE ARRAY wa[n] >>>> >>>> LET w_json =3D TO JSON(wa) >>>> DISPLAY w_json >>>> >>>> END MAIN >>>> >>>> OUTPUT >>>> [{"id": "1", "product": "P01"},{"id": "1", "product": "P01"},{"id": >>>> "1", "product": "P01"}] >>>> >>>> Regards, >>>> >>>> Iwan >>>> _______________________________________________ >>>> Aubit4gl-discuss mailing list >>>> [email protected] >>>> https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss >>>> >>> _______________________________________________ >> Aubit4gl-discuss mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss >> > --00000000000029ea05063ffef92a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"auto">Oops, my mistake. Thanks for=C2=A0pointing that out.</div= ><br><div class=3D"gmail_quote gmail_quote_container"><div dir=3D"ltr" clas= s=3D"gmail_attr">On Tue, Sep 30, 2025, 1:29=E2=80=AFPM Mike Aubury <<a h= ref=3D"mailto:[email protected]">[email protected]</a>> wrote:<br></div><block= quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc= solid;padding-left:1ex"><div dir=3D"ltr">You're using it wrong - you n= eed to use :<div><br><br><br>To deserialize JSON use - LET FROM JSON<br><br= >To serialize JSON - use TO JSON(ws)=C2=A0<br><br>MAIN<br>=C2=A0 =C2=A0DEFI= NE =C2=A0ws CHAR(200)<br>=C2=A0 =C2=A0DEFINE =C2=A0wsNew CHAR(200)<br>=C2= =A0 =C2=A0DEFINE =C2=A0wr RECORD<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0data ARRAY[5] OF CHAR(1)<br>=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 END RECORD<br><br>=C2=A0 =C2=A0LET ws =3D '= { "data": [ "a", "b", "c", "d&= quot;, "e" ] }'<br><br><br>=C2=A0 =C2=A0# Deserialise<br>=C2= =A0 =C2=A0LET FROM JSON wr =3D ws=C2=A0<br><br>=C2=A0 =C2=A0DISPLAY "1= =3D", wr.data[1]<br>=C2=A0 =C2=A0DISPLAY "2=3D", wr.data[2]<= br>=C2=A0 =C2=A0DISPLAY "3=3D", wr.data[3]<br>=C2=A0 =C2=A0DISPLA= Y "4=3D", wr.data[4]<br>=C2=A0 =C2=A0DISPLAY "5=3D", wr= .data[5]<br><br>=C2=A0 =C2=A0# Serialise<br>=C2=A0 =C2=A0LET wsNew=3DTO JSO= N (wr)<br><br>=C2=A0 =C2=A0DISPLAY wsNew clipped<br><br>END MAIN<br><br><br= >So your line=C2=A0<br>=C2=A0=C2=A0LET FROM JSON wr.data =3D TO JSON(ws)<br= ><br>Was serialising the string (which leaves you with a JSON string), then= trying to deserialize the string into the array (which obviously wouldnt= =C2=A0work)<br><br></div></div><br><div class=3D"gmail_quote"><div dir=3D"l= tr" class=3D"gmail_attr">On Tue, 30 Sept 2025 at 01:43, Iwan AB <<a href= =3D"mailto:[email protected]" target=3D"_blank" rel=3D"noreferrer">iw87cs@gm= ail.com</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"= margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-lef= t:1ex"><div dir=3D"ltr">Great. Thank you very much.<div>Btw, I've test = the following code:</div><div><br></div><div>MAIN<br>=C2=A0 =C2=A0DEFINE = =C2=A0ws CHAR(200)<br>=C2=A0 =C2=A0DEFINE =C2=A0wr RECORD<br>=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0data ARRAY[5] OF CHAR(1)<br= >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 END RECORD<br><br>=C2=A0 = =C2=A0LET ws =3D '{ "data": [ "a", "b", &= quot;c", "d", "e" ] }'<br>=C2=A0 =C2=A0LET FRO= M JSON wr.data =3D TO JSON(ws)<br><br>=C2=A0 =C2=A0DISPLAY "1=3D"= , wr.data[1]<br>=C2=A0 =C2=A0DISPLAY "2=3D", wr.data[2]<br>=C2=A0= =C2=A0DISPLAY "3=3D", wr.data[3]<br>=C2=A0 =C2=A0DISPLAY "4= =3D", wr.data[4]<br>=C2=A0 =C2=A0DISPLAY "5=3D", wr.data[5]<= br><br>END MAIN</div><div><br></div><div>the output is:</div><div>1=3D</div= ><div>2=3D</div><div>3=3D</div><div>4=3D</div><div>5=3D</div><div><br></div= ><div>instead of:</div><div><div>1=3Da</div><div>2=3Db</div><div>3=3Dc</div= ><div>4=3Dd</div><div>5=3De</div><div><br></div></div></div><br><div class= =3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, Sep 30, 2025= at 12:38=E2=80=AFAM Mike Aubury <<a href=3D"mailto:[email protected]" targ= et=3D"_blank" rel=3D"noreferrer">[email protected]</a>> wrote:<br></div><bl= ockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-lef= t:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Thats fixed= in SVN now<div><br></div><div>$ cat prog.4gl<br>MAIN<br>=C2=A0 DEFINE =C2= =A0wa DYNAMIC ARRAY OF RECORD<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0id CHAR(2),<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0product CHAR(= 60)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 END RECORD<br>=C2=A0 DEFINE =C2=A0i,= n SMALLINT<br>=C2=A0 DEFINE =C2=A0w_json STRING<br><br>=C2=A0 ALLOCATE ARR= AY wa[10]<br><br><br>=C2=A0 LET wa[1].id=3D1<br>=C2=A0 LET wa[1].product=3D= "Product 1"<br>=C2=A0 LET wa[2].id=3D2<br>=C2=A0 LET wa[2].produc= t=3D"Product 2"<br>=C2=A0 LET wa[3].id=3D3<br>=C2=A0 LET wa[3].pr= oduct=3D"Product 3"<br>=C2=A0 LET wa[4].id=3D4<br>=C2=A0 LET wa[4= ].product=3D"Product 4"<br><br>display wa[1].id<br>display wa[2].= id<br>display wa[3].id<br>display wa[4].id<br><br>=C2=A0 RESIZE ARRAY wa[4]= <br><br>=C2=A0 LET w_json =3D TO JSON(wa)<br>=C2=A0 DISPLAY w_json<br><br>E= ND MAIN<br><br>$ 4glpc -g prog.4gl -o prog.4ae<br>$ ./prog.4ae<br>1<br>2<br= >3<br>4<br>[{"id":"1","product":"Product= 1"},{"id":"2","product":"Product 2= "},{"id":"3","product":"Product 3&q= uot;},{"id":"4","product":"Product 4&quo= t;}]<br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class= =3D"gmail_attr">On Mon, 29 Sept 2025 at 18:04, Iwan AB <<a href=3D"mailt= o:[email protected]" target=3D"_blank" rel=3D"noreferrer">[email protected]</= a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p= x 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><d= iv dir=3D"ltr">Dear Mike, I've tried the JSON operation. It is awesome,= thank you. But if I may suggest, it would be nice, if it could be implemen= ted with DYNAMIC ARRAY also.<br><br>with static array:<br>MAIN<br>=C2=A0 DE= FINE =C2=A0wa ARRAY[5] OF RECORD<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 id CHAR(2),<br>=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 product CHAR(60)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0END RECORD<br>=C2=A0 DEFINE= =C2=A0i, n SMALLINT<br>=C2=A0 DEFINE =C2=A0w_json STRING <br>=C2=A0 <br>= =C2=A0 DECLARE cur1 CURSOR FOR<br>=C2=A0 =C2=A0 =C2=A0SELECT id, product FR= OM t1<br><br>=C2=A0 LET i =3D 1 =C2=A0 =C2=A0 <br>=C2=A0 FOREACH cur1 INTO = wa[i].*<br>=C2=A0 =C2=A0 =C2=A0LET i =3D i + 1<br>=C2=A0 END FOREACH<br>=C2= =A0 =C2=A0 <br>=C2=A0 LET w_json =3D TO JSON(wa)<br>=C2=A0 DISPLAY w_json<b= r><br>END MAIN<br><br>OUTPUT:<br>[{"id": "1", "pro= duct": "P01"},{"id": "2", "product&= quot;: "P02"},{"id": "3", "product"= : "P03"}, {"id": "", "product": &qu= ot;"}, {"id":"", "product": ""= }] =C2=A0<br><br>with dynamic array:<br>MAIN<br>=C2=A0 DEFINE =C2=A0wa DYNA= MIC ARRAY OF RECORD<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0id CHAR(2= ),<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0product CHAR(60)<br>=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 END RECORD<br>=C2=A0 DEFINE =C2=A0i, n SMALLINT<br= >=C2=A0 DEFINE =C2=A0w_json STRING <br>=C2=A0 <br>=C2=A0 ALLOCATE ARRAY wa[= 10]<br>=C2=A0 <br>=C2=A0 DECLARE cur1 CURSOR FOR<br>=C2=A0 =C2=A0 =C2=A0SEL= ECT id, product FROM t1<br><br>=C2=A0 LET i =3D 1 =C2=A0 =C2=A0 <br>=C2=A0 = FOREACH cur1 INTO wa[i].*<br>=C2=A0 =C2=A0 =C2=A0LET i =3D i + 1<br>=C2=A0 = END FOREACH<br>=C2=A0 LET n =3D i - 1<br>=C2=A0 <br>=C2=A0 RESIZE ARRAY wa[= n]<br>=C2=A0 <br>=C2=A0 LET w_json =3D TO JSON(wa)<br>=C2=A0 DISPLAY w_json= <br><br>END MAIN<br><br>OUTPUT<br>[{"id": "1", "pr= oduct": "P01"},{"id": "1", "product= ": "P01"},{"id": "1", "product"= ;: "P01"}]<div><br></div><div>Regards,</div><div><br></div><div>I= wan</div></div> _______________________________________________<br> Aubit4gl-discuss mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank"= rel=3D"noreferrer">[email protected]</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss" r= el=3D"noreferrer noreferrer" target=3D"_blank">https://lists.sourceforge.ne= t/lists/listinfo/aubit4gl-discuss</a><br> </blockquote></div> </blockquote></div> _______________________________________________<br> Aubit4gl-discuss mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank"= rel=3D"noreferrer">[email protected]</a><br> <a href=3D"https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss" r= el=3D"noreferrer noreferrer" target=3D"_blank">https://lists.sourceforge.ne= t/lists/listinfo/aubit4gl-discuss</a><br> </blockquote></div> </blockquote></div> --00000000000029ea05063ffef92a-- --===============7512444516078865788== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============7512444516078865788== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Aubit4gl-discuss mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss --===============7512444516078865788==--