Back to Examples

Parse CSV byte stream to Ballerina record array

The Ballerina data.csv library allows parsing the CSV byte streams as arrays of Ballerina records, making it easier to process CSV data directly from streams. This functionality supports data projection, enabling developers to extract and map only the required fields to the target record type.

import ballerina/data.csv;
import ballerina/io;

type Book record {
    string name;
    string author;
    decimal price;
    string publishedDate;
};

type BookMinimal record {|
    string name;
    string author;
|};

const csvFilePath = "./files/csv_file.csv";

public function main() returns error? {
    // Write a CSV file.
    check io:fileWriteCsv(csvFilePath, [["name", "author", "price", "publishedDate"],
                                        ["Effective Java", "Joshua Bloch", "45.99", "2018-01-01"],
                                        ["Clean Code", "Robert C. Martin", "37.5", "2008-08-01"]]);

    // Read the CSV file as a byte stream.
    stream<byte[], error?> bookStream = check io:fileReadBlocksAsStream(csvFilePath);

    // Parse as CSV byte stream to an array of records.
    Book[] bookArray = check csv:parseStream(bookStream);
    io:println(bookArray);

    bookStream = check io:fileReadBlocksAsStream(csvFilePath);

    // Parse the CSV byte stream as an array of records with data projection.
    // Here only the fields specified in the target record type (`name` and `author` fields)  
    // are included in the constructed value. 
    BookMinimal[] briefBookArray = check csv:parseStream(bookStream);
    io:println(briefBookArray);
}
$ bal run csv_streams_to_record_array.bal[{"name":"Effective Java","author":"Joshua Bloch","price":45.99,"publishedDate":"2018-01-01"},{"name":"Clean Code","author":"Robert C. Martin","price":37.5,"publishedDate":"2008-08-01"}][{"name":"Effective Java","author":"Joshua Bloch"},{"name":"Clean Code","author":"Robert C. Martin"}]
PreviousConvert CSV string to arrays
NextParse CSV lists to custom types