Re: TO JSON with dynamic array
Mike Aubury <[email protected]> Tue, 30 Sep 2025 07:29:43 +0100
| Newsgroups | gmane.comp.lang.4gl.aubit.general |
|---|---|
| Message-ID | <CAGAq4WHqSjO0AcWzkNHmt3-BbJqJR+HsGACNP=es_iafKomnyA@mail.gmail.com> |
--===============3541391075847149070==
Content-Type: multipart/alternative; boundary="0000000000003ebd07063ffee196"
--0000000000003ebd07063ffee196
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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]> wro=
te:
>
>> 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. But
>>> if I may suggest, it would be nice, if it could be implemented with DYN=
AMIC
>>> 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
>
--0000000000003ebd07063ffee196
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">You're using it wrong - you need 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=A0DEFINE =C2=A0ws CHAR(200)<br>=
=C2=A0 =C2=A0DEFINE =C2=A0wsNew CHAR(200)<br>=C2=A0 =C2=A0DEFINE =C2=A0wr R=
ECORD<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 EN=
D RECORD<br><br>=C2=A0 =C2=A0LET ws =3D '{ "data": [ "a&=
quot;, "b", "c", "d", "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=A0DISPLAY "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 JSON (wr)<br><br>=C2=A0 =C2=A0DISP=
LAY 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 stri=
ng (which leaves you with a JSON string), then trying to deserialize the st=
ring into the array (which obviously wouldnt=C2=A0work)<br><br></div></div>=
<br><div class=3D"gmail_quote gmail_quote_container"><div dir=3D"ltr" class=
=3D"gmail_attr">On Tue, 30 Sept 2025 at 01:43, Iwan AB <<a href=3D"mailt=
o:[email protected]">[email protected]</a>> wrote:<br></div><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid=
rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Great. Thank you very =
much.<div>Btw, I've test the following code:</div><div><br></div><div>M=
AIN<br>=C2=A0 =C2=A0DEFINE =C2=A0ws CHAR(200)<br>=C2=A0 =C2=A0DEFINE =C2=A0=
wr RECORD<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
data 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": [ &q=
uot;a", "b", "c", "d", "e" ] }=
'<br>=C2=A0 =C2=A0LET FROM 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=A0DISPLA=
Y "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</di=
v><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"gm=
ail_attr">On Tue, Sep 30, 2025 at 12:38=E2=80=AFAM Mike Aubury <<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>> wrote:<=
br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8e=
x;border-left: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=A0pr=
oduct 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 DEFIN=
E =C2=A0i, n SMALLINT<br>=C2=A0 DEFINE =C2=A0w_json STRING<br><br>=C2=A0 AL=
LOCATE ARRAY 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].product=3D"Product 2"<br>=C2=A0 LET wa[3].id=3D3<br>=C2=A0 LE=
T wa[3].product=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>di=
splay 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_j=
son<br><br>END 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":&=
quot;Product 1"},{"id":"2","product":&qu=
ot;Product 2"},{"id":"3","product":"=
;Product 3"},{"id":"4","product":"P=
roduct 4"}]<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 hre=
f=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>> wr=
ote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px=
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D=
"ltr">Dear Mike, I've tried the JSON operation. It is awesome, thank yo=
u. But if I may suggest, it would be nice, if it could be implemented with =
DYNAMIC ARRAY also.<br><br>with static array:<br>MAIN<br>=C2=A0 DEFINE =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=A0=
i, n SMALLINT<br>=C2=A0 DEFINE =C2=A0w_json STRING <br>=C2=A0 <br>=C2=A0 DE=
CLARE cur1 CURSOR FOR<br>=C2=A0 =C2=A0 =C2=A0SELECT id, product FROM t1<br>=
<br>=C2=A0 LET i =3D 1 =C2=A0 =C2=A0 <br>=C2=A0 FOREACH cur1 INTO wa[i].*<b=
r>=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<br><br>E=
ND MAIN<br><br>OUTPUT:<br>[{"id": "1", "product&qu=
ot;: "P01"},{"id": "2", "product": =
"P02"},{"id": "3", "product": "=
;P03"}, {"id": "", "product": "&quo=
t;}, {"id":"", "product": ""}] =C2=
=A0<br><br>with dynamic array:<br>MAIN<br>=C2=A0 DEFINE =C2=A0wa DYNAMIC AR=
RAY 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),<b=
r>=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=A0SELECT =
id, product FROM t1<br><br>=C2=A0 LET i =3D 1 =C2=A0 =C2=A0 <br>=C2=A0 FORE=
ACH 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]<b=
r>=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", "produc=
t": "P01"},{"id": "1", "product&quo=
t;: "P01"},{"id": "1", "product": &=
quot;P01"}]<div><br></div><div>Regards,</div><div><br></div><div>Iwan<=
/div></div>
_______________________________________________<br>
Aubit4gl-discuss mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss" r=
el=3D"noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/lis=
tinfo/aubit4gl-discuss</a><br>
</blockquote></div>
</blockquote></div>
_______________________________________________<br>
Aubit4gl-discuss mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/aubit4gl-discuss" r=
el=3D"noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/lis=
tinfo/aubit4gl-discuss</a><br>
</blockquote></div>
--0000000000003ebd07063ffee196--
--===============3541391075847149070==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============3541391075847149070==
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
--===============3541391075847149070==--