Hello there. I am having trouble with the csvtojson package. I have a csv-file with about 2500 lines. The delimiter is tab ( \t ).The following code which ,just reads the csv, worked just fine a few weeks ago (please note that this is a method and not a function). At this point i would have a perfectly parsed JSON Object stored in my variable :extractReport: function(filePath) { return csvtojson({delimiter: "\t"}) .fromFile(filePath) .then((jsonResult) => { return jsonResult }) } Now the file is a lot bigger than before and i tried to solve the problem with streams. The stream so far is a lot faster while just reading and writing than fs.readFile and fs.writeFile with promises.However i am having trouble with the csv-converter implementation for it (which should store the resulting JSON Object inside a file). The following code just copies the file to the destination while converting each line to json.extractReport: function(readFrom, writeTo) { const csvConverter = csv() const readStream = fs.createReadStream(readFrom); const writeStream = fs.createWriteStream(writeTo); readStream.pipe(csvConverter).pipe(writeStream); } But it does not create object properties with the headers, it just copies the headers to each object. And as soon as i set the property delimiter: "\t" inside the csv parser like so:csv({delimiter: "\t"}) the code just stops working and leaves the destination file empty without trowing any errors. Am i making a mistake somewhere in my code ?Edit:Here is an example of what happens when i run the code with streams:Lets assume i have the following csv with four whitespaces between the values as the tab delimiter: Name Age Gender John Doe Male The result is as follows:{"Name\tAge\tGender":"John\tDoe\tMale"} It looks like it just takes the whole header string and converts it to the property name and sets every following row with the actual data to the value of the property.
Submitted September 05, 2018 at 01:31PM by NamkoBanzai
No comments:
Post a Comment