-
Notifications
You must be signed in to change notification settings - Fork 0
How to use
n4n5 edited this page Jul 11, 2021
·
3 revisions
Just import the function and use it !
const converter = require("csv-to-custom-json")
const doConversion = async () => {
const result = await converter("myfile.csv");
}
doConversion();
We nned to import the front version :
import converter from "csv-to-custom-json/front"
const doConversion = async () => {
// we pass an array wich contains all lines
const result = await converter([
"num1,num2,num3,num4",
"1,2,3,4",
"4,5,6,7",
"7,8,9,10"
]);
}
doConversion();
Legend :
- the difference with the normal file :
- no
fs
- no
readline
- we pass an array of line and not a link
Create a schema variable and put it as second parameter !
Exemple with a simple csv
:
num1,num2,num3
1,2,3
4,5,6
7,8,9
const schema = {
num1: "string",
num2(item) {
return null;
},
num3: "int"
};
const result = await converter("myfile.csv");
Legend :
- ad you can see the schema can contains function, or string with the type
- the values with type will be parsed
- attribute of the object are the word in the first line of the csv
It's the same as a simple schema :
const schema = {
obj1: {
obj2: {
num4: "string"
}
},
num2: "",
num3: ""
};
const result = await converter("myfile.csv");
If you want to check some real case, check out the folder test
in the GitHub repository
If you want to see and use options check that documentation : How-to-options