-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5669 from SasinduDilshara/parse-csv-string-to-any…
…data-array Add bbe for converting csv string to `anydata[][]`
- Loading branch information
Showing
5 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
examples/csv-string-to-anydata-array/csv_string_to_anydata_array.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/csv-string-to-anydata-array/csv_string_to_anydata_array.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ::: |
2 changes: 2 additions & 0 deletions
2
examples/csv-string-to-anydata-array/csv_string_to_anydata_array.metatags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 4 additions & 0 deletions
4
examples/csv-string-to-anydata-array/csv_string_to_anydata_array.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters