简体   繁体   中英

How to iterate an object from the npm package “csvtojson”

I have a problem converting my csv to json with the npm package "csvtojson". First, I converted it into an ascii character set, then I used the conversion to utf16 offered by the package, but now they appear to me as "[object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object] "

I tried to iterate the object, but without success. I need your help. Then I leave an image of the csv.

在此处输入图片说明

const csvFilePath='./download/negocio.csv'
const csv=require('csvtojson');

app.get('/api', async(req, res) => {
  var jsonArray=await csv().fromFile(csvFilePath);
  jsonArray =  jsonArray.toString('utf16');

  res.json(jsonArray);

});

Try this first

const utils = require('../helper/util');

const path = './download/negocio.csv';

app.get('/api', async (req, res) => {
  const result = await utils.csvToJson(path);
  return res.status(200).json(res);
});

utils.js

const csv = require('csvtojson');

const utils = {};

utils.csvToJson = async (path) => {
  try {
    const jsonArray = await csv().fromFile(path);
    return jsonArray;
  } catch (err) {
    throw err;
  }
};

module.exports = utils;

Note keeps eyes on the path. The path ./downloads/.. can be different for one file to another file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM