Reputation: 53
I would like to read a csv file and convert into JSON array of object so I can insert into rethinkdb database. I am working in node js.
A tool called csvtojson convert the csv content into json (see code here below). I tried to put the result into the variable called database. However, the database is still empty in console.log. Do you know why this is the case?
//Converter Class
var Converter = require("csvtojson").Converter;
var converter = new Converter({});
//read from file
require("fs").createReadStream("test.csv").pipe(converter);
var database={};
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function (jsonArray) {
console.log(jsonArray); //here is your result jsonarray
database=jsonArray
});
console.log(JSON.stringify(database))
My output from the test.csv in the console.log is:
[ { Name: 'object 1', Quantity: -100 }, { Name: 'object 2', Quantity: -1750 } ]
My second question:
Is this good enough to insert in the rethinkdb or should I convert into another format for it to work?
Thanks!!
Upvotes: 0
Views: 303
Reputation: 2314
It's because JSON.stringify(database)
run before event end_parsed
fired. In NodeJS, the flow control isn't like in other language, meaning top to bottom, because of JavaScript async. After setting event handler for event end_parsed
of converter object. Your program continue to run, without blocking to wait for processing CSV file. At that time, database
is still empty.
Simply change it like this:
converter.on("end_parsed", function (jsonArray) {
console.log(jsonArray); //here is your result jsonarray
database=jsonArray
console.log(JSON.stringify(database))
});
will make it works, because database
is set and call to console.log inside event handler of end_parsed
event.
Is this good enough to insert in the rethinkdb or should I convert into another format for it to work?
Yes, it's. Though a minor point, is probably you should use key like name
, quantity
to match with many other JavaScript style which is usually camelCase.
Also, you should read here https://rethinkdb.com/docs/importing/
to see if you can just use RethinkDB import to import CSV file directly. I found that CSV import is a bit limited but probably just give it a try.
Upvotes: 1