If I have a Json array, how can I find number of objects inside that array?

C#:
/* Include the necessary namespace for JsonArray */
USING Progress.Json.ObjectModel.*.

DEFINE TEMP-TABLE ttFruit
  FIELD fruit    AS CHARACTER  .

DEFINE VARIABLE cCharExtent AS character  NO-UNDO extent 4 INITIAL ["Apple","Banana","Cherry","Date"].
DEFINE VARIABLE oJsonArray  AS JsonArray NO-UNDO.
DEFINE VARIABLE iArrayLength AS INTEGER   NO-UNDO.

procedure charExtentToTT PRIVATE:

    DEFINE input parameter cCharExtent as character no-undo extent.

    DEFINE VARIABLE iLoop as integer no-undo.

    do iLoop = 1 to extent(cCharExtent):
        create ttFruit.
        Assign ttFruit.fruit = cCharExtent[iLoop].
    end.

    return.
end procedure.

run charExtentToTT (input  cCharExtent).

oJsonArray = NEW JsonArray().
oJsonArray:Read(TEMP-TABLE ttFruit:HANDLE).

/* Get the length of the JSON array */
iArrayLength = oJsonArray:Length.


MESSAGE "The length of the JSON array is:" iArrayLength
    VIEW-AS ALERT-BOX.
 
Simplified the code sample.

C#:
/* Include the necessary namespace for JsonArray */
USING Progress.Json.ObjectModel.*.

DEFINE VARIABLE myParser AS ObjectModelParser NO-UNDO.
DEFINE VARIABLE oJsonArray  AS JsonArray NO-UNDO.
DEFINE VARIABLE iArrayLength AS INTEGER   NO-UNDO.
DEFINE VARIABLE myJsonString AS CHARACTER INITIAL  '["Apple","Banana","Cherry","Date"]'.

myParser = NEW ObjectModelParser().
oJsonArray = CAST(myParser:Parse(myJsonString ), JsonArray).

/* Get the length of the JSON array */
iArrayLength = oJsonArray:Length.


MESSAGE "The length of the JSON array is:" iArrayLength
    VIEW-AS ALERT-BOX.

 
Back
Top