import ballerina/io;type Employee {
    int id;
    string name;
    float salary;
};function main (string[] args) {
    table < Employee> tb = table {};
    Employee e1 = {id:1, name:"Jane", salary:300.50};
    Employee e2 = {id:2, name:"Anne", salary:100.50};
    Employee e3 = {id:3, name:"John", salary:400.50};
    Employee e4 = {id:4, name:"Peter", salary:150.0};    tb.add(e1);
    tb.add(e2);
    tb.add(e3);
    tb.add(e4);
    io:print("Table Data:");
    io:println(tb);
    foreach x in tb {
        io:println("Name: " + x.name);
    }
    float lowerAvgSal = tb.filter(isLowerSalary).map(getSalary).average();
    io:println("Average of Low salary:" + lowerAvgSal);
    int count = tb.remove(isLowerSalary);
    io:println("Deleted row count:" + count);
    io:print("After Delete:");
    io:println(tb);
    var j = <json> tb;    match j {
        json jsonRes => {
                       io:print("JSON:");
                       io:println(j);
                   }
        error err =>  io:println("error: " + err.message);
    }
    var x = <xml> tb;    match x {
        xml xmlRes => {
                   io:print("XML:");
                   io:println(x);
               }
        error err =>  io:println("error: " + err.message);
    }
}function isLowerSalary (Employee p) returns (boolean) {
    return p.salary < 200;
}function getSalary (Employee p) returns (float) {
    return p.salary;
}

Table

The table type is used to hold tabular data and in memory table can be created with a given struct constraint.

import ballerina/io;
type Employee {
    int id;
    string name;
    float salary;
};
function main (string[] args) {
    table < Employee> tb = table {};

Create in memory table constrained by the Employee struct type.

    Employee e1 = {id:1, name:"Jane", salary:300.50};
    Employee e2 = {id:2, name:"Anne", salary:100.50};
    Employee e3 = {id:3, name:"John", salary:400.50};
    Employee e4 = {id:4, name:"Peter", salary:150.0};

Add some data rows to the table.

    tb.add(e1);
    tb.add(e2);
    tb.add(e3);
    tb.add(e4);
    io:print("Table Data:");
    io:println(tb);

Print the table data.

    foreach x in tb {
        io:println("Name: " + x.name);
    }

Access using foreach.

    float lowerAvgSal = tb.filter(isLowerSalary).map(getSalary).average();
    io:println("Average of Low salary:" + lowerAvgSal);

Find the average salary using iterable operations.

    int count = tb.remove(isLowerSalary);
    io:println("Deleted row count:" + count);
    io:print("After Delete:");
    io:println(tb);

Delete rows matching to a given criteria.

    var j = <json> tb;

Convert to a json.

    match j {
        json jsonRes => {
                       io:print("JSON:");
                       io:println(j);
                   }
        error err =>  io:println("error: " + err.message);
    }
    var x = <xml> tb;

Convert to a xml.

    match x {
        xml xmlRes => {
                   io:print("XML:");
                   io:println(x);
               }
        error err =>  io:println("error: " + err.message);
    }
}
function isLowerSalary (Employee p) returns (boolean) {
    return p.salary < 200;
}
function getSalary (Employee p) returns (float) {
    return p.salary;
}
$ ballerina run table-with-sql-connector.bal
Table Data:{data: [{id:1, name:"Jane", salary:300.5}, {id:2, name:"Anne", salary:100.5}, {id:3, name:"John", salary:400.5}, {id:4, name:"Peter", salary:150.0}]}
Name: Jane
Name: Anne
Name: John
Name: Peter
Average of Low salary:125.25
Deleted row count:2
After Delete:{data: [{id:1, name:"Jane", salary:300.5}, {id:3, name:"John", salary:400.5}]}
JSON:[{"id":1,"name":"Jane","salary":300.5},{"id":3,"name":"John","salary":400.5}]
XML:<results><result><id>1</id><name>Jane</name><salary>300.5</salary></result><result><id>3</id><name>John</name><salary>400.5</salary></result></results>