TO JSON with dynamic array

Iwan AB <[email protected]> Tue, 30 Sep 2025 00:03:57 +0700
Newsgroups gmane.comp.lang.4gl.aubit.general
Message-ID <CAHJCSmKVkOqRXM_ddaLiufPJEf-9vcYZqnekdVapsxiigQ6zdQ@mail.gmail.com>
--===============7432269092990959727==
Content-Type: multipart/alternative; boundary="000000000000ae3c84063ff39f4c"

--000000000000ae3c84063ff39f4c
Content-Type: text/plain; charset="UTF-8"

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 DYNAMIC
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 = 1
  FOREACH cur1 INTO wa[i].*
     LET i = i + 1
  END FOREACH

  LET w_json = 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 = 1
  FOREACH cur1 INTO wa[i].*
     LET i = i + 1
  END FOREACH
  LET n = i - 1

  RESIZE ARRAY wa[n]

  LET w_json = TO JSON(wa)
  DISPLAY w_json

END MAIN

OUTPUT
[{"id": "1", "product": "P01"},{"id": "1", "product": "P01"},{"id": "1",
"product": "P01"}]

Regards,

Iwan

--000000000000ae3c84063ff39f4c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Dear Mike, I&#39;ve tried the JSON operation. It is awesom=
e, thank you. But if I may suggest, it would be nice, if it could be implem=
ented 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 DEFIN=
E =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>[{&quot;id&quot;: &quot;1&quot;, &quot;pro=
duct&quot;: &quot;P01&quot;},{&quot;id&quot;: &quot;2&quot;, &quot;product&=
quot;: &quot;P02&quot;},{&quot;id&quot;: &quot;3&quot;, &quot;product&quot;=
: &quot;P03&quot;}, {&quot;id&quot;: &quot;&quot;, &quot;product&quot;: &qu=
ot;&quot;}, {&quot;id&quot;:&quot;&quot;, &quot;product&quot;: &quot;&quot;=
}] =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>[{&quot;id&quot;: &quot;1&quot;, &quot;pr=
oduct&quot;: &quot;P01&quot;},{&quot;id&quot;: &quot;1&quot;, &quot;product=
&quot;: &quot;P01&quot;},{&quot;id&quot;: &quot;1&quot;, &quot;product&quot=
;: &quot;P01&quot;}]<div><br></div><div>Regards,</div><div><br></div><div>I=
wan</div></div>

--000000000000ae3c84063ff39f4c--


--===============7432269092990959727==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============7432269092990959727==
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

--===============7432269092990959727==--