0

I am using node js lib "csvtojson" to read csv files and convert to json. in my project folder i have 2 csv files which i am trying to read and convert to json and send the json as a response . Below is the csv content. Both the files having same data

enter image description here

The issue which i am getting is after the first file read is finished , its reading the 1st row of 2nd file which is the column header and displaying the json like below

 {
        "Col 1": "Col 1", 
        "Col 2": "Col 2",
        "Col 3": "Col 3",
        "Col 4": "Col 4"
    }

JSON data

[
    {
        "Col 1": "Test 1",
        "Col 2": "ABC 1",
        "Col 3": "DEF 1",
        "Col 4": "GHI 1"
    },
   {
        "Col 1": "Test 2",
        "Col 2": "ABC 2",
        "Col 3": "DEF 2",
        "Col 4": "GHI 2"
    }, 
    {
        "Col 1": "Test 3",
        "Col 2": "ABC 3",
        "Col 3": "DEF 3",
        "Col 4": "GHI 3"
    },
    {
        "Col 1": "Col 1", // **Issue is here**
        "Col 2": "Col 2",
        "Col 3": "Col 3",
        "Col 4": "Col 4"
    },
     {
        "Col 1": "Test 1",
        "Col 2": "ABC 1",
        "Col 3": "DEF 1",
        "Col 4": "GHI 1"
    },
   {
        "Col 1": "Test 2",
        "Col 2": "ABC 2",
        "Col 3": "DEF 2",
        "Col 4": "GHI 2"
    }, 
    {
        "Col 1": "Test 3",
        "Col 2": "ABC 3",
        "Col 3": "DEF 3",
        "Col 4": "GHI 3"
    }
]

Below is my code

import dotenv from 'dotenv';
import express from 'express';
import { Router } from 'express';
import fs from 'fs';
import { join } from "path";
import path from 'path';
import Converter from "csvtojson";

import fastcsv from 'fast-csv';

import Promise from 'bluebird';

Promise.promisifyAll(Converter.prototype);

var converter = new Converter();

const router = Router();
dotenv.config();

router.get("/readfile", (req, res, next) => {
    const currDir = path.join('./outputfolder/');
    console.log('currdir', currDir)

    function readFiles(dir, processFile) {
        // read directory
        fs.readdir(dir, (error, fileNames) => {
            if (error) throw error;

            fileNames.forEach(filename => {
                // get current file name
                const name = path.parse(filename).name;
                // get current file extension
                const ext = path.parse(filename).ext;
                // get current file path
                const filepath = path.resolve(dir, filename);

                // get information about the file
                fs.stat(filepath, function (error, stat) {
                    if (error) throw error;

                    // check if the current path is a file or a folder
                    const isFile = stat.isFile();

                    // exclude folders
                    if (isFile) {
                        // callback, do something with the file
                        processFile(filepath, name, ext, stat);
                    }
                });
            });
        });
    }

    readFiles(currDir, (filepath, name, ext, stat) => {
        // console.log('file path:', filepath);
        console.log('file name:', name);
        let csvData = [];
        converter
            .fromFile(filepath)
            .then((jsonObj) => {
                console.log(jsonObj);
                jsonObj.forEach(res => {
                    if (res.UID !== '') {
                        fs.writeFileSync('2pac.txt', JSON.stringify(jsonObj), (err) => {
                            // throws an error, you could also catch it here
                            if (err) throw err;
                        });
                    }
                })
                res.send(jsonObj)
            })

    });

})

export { router };
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
user2542953
  • 127
  • 1
  • 9

0 Answers0