CSV module

The CSV module contains functions for reading and writing CSV data.

Serialize

Signature: Serialize(input: sequence<{}>, delimiter: text) -> text

Serialize the given sequence of records to CSV that uses the given delimiter character to separate the CSV fields. The resulting data will always contain a header line as it first line.

Fields containing may be escaped with double quotation character as specified in RFC 4180.

Only sequences of records containing fields with Simple types (except binary) can be converted to CSV text. Using unsupported types in the serialization will return a empty text as its result.

let test = [
    {"name": "Pancho", "age": 27}, 
    {"name": "Beatrix", "age": 66}
];

let csvData = CSV.Serialize(test, ","); // csvData has type text

return csvData
// Will generate the following data
//    name,age
//    Pancho,27
//    Beatrix,66
//

Deserialize

Signature: Deserialize<T> (data: text, delimiter: text) -> T*

Deserialize the given CSV data using the specified delimiter into a sequence of records where the type of the record is specified by T. The deserialize function only supports CSV data where the first line is a header line containing a name that corresponds to each field in a line.

let test = "name,age\nPancho,27\nBeatrix,66"
type User = {
    name: text,
    age: number
}

let users = CSV.Deserialize<User>(test, ",");

return users
// Will return [
//    {"name": "Pancho", "age": 27}, 
//    {"name": "Beatrix", "age": 66}
// ];

Last updated