Skip to content

Commit

Permalink
Merge pull request #5669 from SasinduDilshara/parse-csv-string-to-any…
Browse files Browse the repository at this point in the history
…data-array

Add bbe for converting csv string to `anydata[][]`
  • Loading branch information
gimantha authored Sep 27, 2024
2 parents 0bd300e + 999e036 commit eca4bea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ballerina/data.csv;
import ballerina/io;

public function main() returns error? {
string csvString = string `name,author,price,publishedDate
Effective Java,Joshua Bloch,45.99,2018-01-01
Clean Code,Robert C. Martin,37.50,2008-08-01`;

// Parse the CSV string as an array of `string` arrays.
string[][] bookArray = check csv:parseString(csvString);
io:println(bookArray);

// Parse the CSV string to an array of `anydata` arrays.
anydata[][] bookArray2 = check csv:parseString(csvString);
io:println(bookArray2);

// Parse the CSV string to an array of `string` arrays with data projection.
// Here only the first two headers are extracted to the resulting array,
// based on the array member size specified in the expected type.
string[][2] bookArray3 = check csv:parseString(csvString);
io:println(bookArray3);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Parse CSV string to arrays

The Ballerina `data.csv` library offers a number of functions for parsing CSV strings to subtypes of `anydata[][]`, enabling developers to effectively parse CSV string data into flexible data structures. Additionally, it provides the option to select specific fields for inclusion in the resulting arrays of `anydata` arrays, allowing developers to extract only the required data.

::: code csv_string_to_anydata_array.bal :::

::: out csv_string_to_anydata_array.out :::
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates the parsing of a CSV string into an array of anydata arrays, both with and without data projection.
keywords: ballerina, ballerina by example, bbe, csv, csv to array, csv string, anydata, anydata array, parseString, csv data module, data.csv, data projection
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ bal run csv_string_to_anydata_array.bal
[["Effective Java","Joshua Bloch","45.99","2018-01-01"],["Clean Code","Robert C. Martin","37.50","2008-08-01"]]
[["Effective Java","Joshua Bloch",45.99,"2018-01-01"],["Clean Code","Robert C. Martin",37.5,"2008-08-01"]]
[["Effective Java","Joshua Bloch"],["Clean Code","Robert C. Martin"]]
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4732,6 +4732,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Convert CSV string to arrays",
"url": "csv-string-to-anydata-array",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
}
]
},
Expand Down

0 comments on commit eca4bea

Please sign in to comment.