import ballerina/io;function main (string[] args) {
    int[] intArray = [1, 2, 3];    int arrayLength;
    arrayLength = lengthof intArray;
    io:println("Integer array size : " + arrayLength);
    json jsonArray = [{"name":"John", "age":31},
                      {"name":"Neal", "age":22}];
    arrayLength = lengthof jsonArray;
    io:println("JSON array size : " + arrayLength);
}

Lengthof

Ballerina supports 'lengthof' which returns the length of an array.

import ballerina/io;
function main (string[] args) {
    int[] intArray = [1, 2, 3];

Here you create integer array with with several integer elements.

    int arrayLength;
    arrayLength = lengthof intArray;
    io:println("Integer array size : " + arrayLength);

Print the length of the created integer array calculated by the ‘lengthof’ unary operator.

    json jsonArray = [{"name":"John", "age":31},
                      {"name":"Neal", "age":22}];

Here you create JSON array with several JSON elements.

    arrayLength = lengthof jsonArray;
    io:println("JSON array size : " + arrayLength);
}

Print the length of the created JSON array calculated by the ‘lengthof’ unary operator.

$ ballerina run lengthof.bal
Integer array size : 3
JSON array size : 2