GetJsonFromCsv()
The GetJsonFromCsv() function allows to convert a CSV table (from a file in the case or passed as string) into a JSON object with an array of objects representing subsequent rows from the table. Values of particular cells are available as properties of these objects.
Syntax
GetJsonFromCsv(fileName); GetJsonFromCsv(fileName, options); GetJsonFromCsv(fieldName); GetJsonFromCsv(fieldName, options); GetJsonFromCsv(csv); GetJsonFromCsv(csv, options);
Function arguments
- fileName – (String) a name of the file from the case documents list (must be .csv or .txt file);
- fieldName – (String) a case field name (must be the document type field with .csv or .txt file);
- csv – (String) a content of some CSV document saved as string;
- options – (Object) [optional] a complex object with properties to define details of conversion:
- options.headerNamesSource – (String) [optional] a property that specifies how headers will be named, available values: 'firstRow’ (default) – first row contains header names; 'autogenerated’ – header names are generated according to pattern: column1,column2… ; 'custom’ – header names are set based on values from property 'customHeaderNames’;
- options.customHeaderNames – (String) [optional] a list of the custom header names for columns in CSV table separated by semicolon (’;’);
- options.sepatator – (String) [optional] this property defines a character of the separator between cells. Available separators: ’;’ (default), ’:’, ’,’ and '\t’ (tabulator);
- options.skipFirstRow – (Boolean) [optional] this property determines whether the first row will be removed from output JSON with set of rows. It can be useful when option 'headerNamesSource’ is set to 'firstRow’. Default value is true;
- options.inputFileEncoding – (String) [optional] this property specifies what character encoding the input file supports. Available values: 'utf8′ (default), 'ascii’, 'windows1252′, 'windows1250′ (encoding of most Polish users’ OS), 'unicode’, 'utf32′ and 'utf7′.
Return value
The GetJsonFromCsv() function returns a string with a JSON object. It contains the property 'Status’ (available values: 'OK’ and 'Error’) which determines whether operation succeded or not. In case of success there are additional properties: 'Rows’ – contains array of rows and each row contains properties called like defined header names and 'RowsNumber’ – number of rows. In case of failure there is property 'ErrorMessage’ which describes the reason of the error.
Examples
Example 1
The function takes CSV table from the file from the list of documents and converts it into a JSON object with rows using default settings (second argument is not set).
GetJsonFromCsv("document.csv");
Example 2
The function takes CSV table from the file located in the field named 'csvDocument’ and converts it into a JSON object with rows using default settings (second argument is not set).
GetJsonFromCsv([csvDocument]);
Example 3
The function takes CSV table passed as a plain text and converts it into a JSON object with rows using default settings (second argument is not set).
GetJsonFromCsv('firstName;lastName\r\nJohn;Smith'); // \r\n indicates a new line character
Example 4
The function takes CSV table from the file from the list of documents and converts it into a JSON object with rows using settings specified in the second argument.
options.headerNamesSource="autogenerated"; options.sepatator=":"; options.skipFirstRow=true; options.inputFileEncoding="windows1250"; GetJsonFromCsv("document.csv", options);