Getting and Setting the Size of a JSON Array

The size of a JSON array is the number of elements. As JSON arrays are zero terminated, the size of a JSON array is always the last index + 1.

JsonGetArrayLength

The function JsonGetArrayLength returns the number of elements in a JSON array.

transaction TMain
var
  jsonArray   : number;
  arrayLength : number;
begin
  jsonArray := JsonParse("[0,1,2,3,4]");

  arrayLength := JsonGetArrayLength(jsonArray);

  Print("Length: " + string(arrayLength));
  JsonFree(jsonArray);
end TMain;

Output
Length: 5

JsonArrayResize

This function changes the size of a JSON array. If the specified size is lower than the actual size, the last elements of the JSON array are removed. If the specified size is higher than the actual size, empty elements are added to the end of the JSON array.

transaction TMain
var
  jsonText  : string;
  jsonArray : number;
begin
  jsonArray := JsonParse("[0,1,2,3,4]");
  
  JsonArrayResize(jsonArray, 2);

  JsonToText(jsonArray, jsonText);
  Print("resized array: " + jsonText);

  JsonFree(jsonArray);
end TMain;

Output
resized array: [0,1]