actual-csv-to-json-parser
This tool converts from CSV to Object arrays, or JSON. It can do TSV too. Here's a super basic example.
-
Install
npm i actual-csv-to-json-parser
-
Call Function
;;console.lognumbers // --> { name: "ten", num: 10, even: true }console.lognumbers // --> { name: "fifteen", num: 15, even: false }--alternatively--
;;;;items.forEachconsole.logitem;--or as an extreme example--
;;;;// all the bells and whistles;// load a csv file, modify it, save it as jsonparser.convertFileToJsonFileSynccsvLoadPath, jsonSavePath,
See "Overview of Functions" for more
Features
- Simple one liner functions
- Turns CSV into 2d String Array, JSON, or Object Array
- Handles quotes and commas just fine
- Can parse primitives (number, string, array, boolean, object)
- Can take raw CSV text
- Can do synchronous and asynchronous file loading
- Can set special delimiters (ie tabs for TSV)
- Typescript
- 150 lines
- That's it.
If you want a typescript interface of your json object, check out this tool: http://json2ts.com/
Overview of Functions
More later, but this is the basics of what exists
Default quick functions
- Creating Object Arrays
csvTextToObjectArray(rawCsvText)
async csvFileToObjectArray(csvFilePath)
csvFileToObjectArraySync(csvFilePath)
- Creating 2D String Arrays
csvTextTo2dArray(rawCsvText)
async csvFileTo2dArray(csvFilePath)
csvFileTo2dArraySync(csvFilePath)
- Creating JSON strings/files
csvTextToJsonText(rawCsvText)
async csvFileToJsonFile(csvFilePath, jsonSavePath)
csvFileToJsonFileSync(csvFilePath, jsonSavePath)
Custom specialty stuff. Same as above but with customized csv parser args
- ICsvParserArgs { delimiter, jsonTabSize }
new CsvParser(customizationArgs)
this.rawTextTo2dArray(rawCsvText)
this.fileTo2dArray(csvFilePath)
this.fileTo2dArraySync(csvFilePath)
this.rawTextToObjectArray(rawCsvText)
this.fileToObjectArray(csvFilePath)
this.fileToObjectArraySync(csvFilePath)
this.rawTextToJsonText(rawCsvText)
this.convertFileToJsonFile(csvFilePath, jsonSavePath)
this.convertFileToJsonFileSync(csvFilePath, jsonSavePath)
What's a CSV or JSON or Object Array?
-
This is a CSV (Comma Separated Values):
name, age, eyesight bob, 21, 20/20 julie, 27, 21/19 mark, 42, 17/17
-
This is an 2D String Array
-
This is an object array (probably what you want)
-
This is json (JSON is a string)
Documentation
look at the source code. It's a single ~250 line file.
If you can read typescript, justThis data is used in the examples below
// imagine raw text also found at /path/to/file.csv;
Creating Object Arrays
-
csvTextToObjectArray(rawCsvText: string)
Creates an object array from csv. Most useful function, likely. The
IExampleData
templating is optional.;/* items === [{a: "1", b: "2"},{a: "x", b: "y"}]*/
-
csvFileToObjectArray(csvFilePath: string)
Asynchronously load a file, turn it into an array of objects. The
IExampleData
templating is optional.;csvFileToObjectArraycsvLoadPath.thenconsole.logitems;// or;
-
csvFileToObjectArraySync(csvFilePath: string)
Synchronously load a file, turn it into an array of objects. The
IExampleData
templating is optional.;;
Creating 2D String Arrays
-
csvTextTo2dArray(rawCsvText: string)
Creates a 2d array from raw csv text
;/* array2d === [["a", "b"],["1", "2"],["x", "y"],]*/
-
csvFileTo2dArray(csvFilePath: string)
Asynchronously creates a 2d array from a csv file
;csvFileTo2dArraycsvLoadPath.thenconsole.logarray2d;// or;
-
csvFileTo2dArraySync(csvFilePath: string)
Synchronously creates a 2d array from a csv file
;;
Creating JSON string
-
csvTextToJsonText(rawCsvText: string)
Turn raw csv text into json text
;/* json === `[{"a": "1", "b": "2"},{"a": "x", "b": "y"}]`*/
Direct conversion from .csv to .json files
-
csvFileToJsonFile(csvFilePath: string, jsonSavePath: string)
Asynchronously load a file, convert it to json, then save it. The
IExampleData
templating is optional. (optional). You can add a function to parse the object array before saving it;csvFileToJsonFilecsvLoadPath, jsonSavePath.thenconsole.log"File saved"// or (with conversion)await csvFileToJsonFilecsvLoadPath, jsonSavePath,;
-
csvFileToJsonFileSync(csvFilePath: string, jsonSavePath: string)
Synchronously load a file, convert it to json, then save it. The
IExampleData
templating is optional. (optional). You can add a function to parse the object array before saving it;csvFileToJsonFileSynccsvLoadPath, jsonSavePath;// or (with conversion)csvFileToJsonFileSynccsvLoadPath, jsonSavePath,;
Special Cases
-
ICsvParserArgs
All the special setting for the parser.
-
new CsvParser(args: ICsvParserArgs)
Create a special csv parser, with custom rules
;;
TSV
Search for CsvParser()
. It has a delimiter argument.
One of the Tests
test`Doesn't break on quotes or commas. Handles primitives and json.`,
Rant
This isn't so much a rant, I'm just going to say I didn't much like the other csv tools. Here's the best two I could find:
-
- Unpacked Size 8.69 MB, Total Files 393, Dependencies 3.
- Does way too much, difficult to figure how to do simple things.
- Is cool once you figure it out, I think.
-
- Unpacked Size 249kB, Total Files 21, Dependencies 0. 👍
- First feature is "Easy to use"
- Doesn't show an example.
- Doesn't seem to have typescript typings... does it do promises or callbacks?
- Probably good, but the npm docs just aren't doing it justice..
Then there's like 100 more.