0

i;m new to node js i need to convert node js file to JSON File it is possible i try

const express = require("express");
const csv = require("csvtojson");
const app = express();
const port = 3000;
const csvFilePath='./csv/HeatMapInput.csv'

app.get("/csvtojson", (req, res) => {
    csv()
    .fromFile(csvFilePath)
    .then((jsonObj)=>{
        const obj = JSON.parse(jsonObj)
     
        res.send(obj)
        
        console.log(jsonObj);
    })
    
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

the result of output is given like : [{"DAUID":"24770084","nbvisitors":"5","longitude":"-74.148441882693675","latitude":"45.8931201053586"},{"DAUID":"24650304","nbvisitors":"5","longitude":"-73.734375249335187","latitude":"45.540948335709679"},{"DAUID":"24661491","nbvisitors":"5","longitude":"-73.606149772501126","latitude":"45.55292693893162"},{"DAUID":"24650268","nbvisitors":"5","longitude":"-73.724700903473192","latitude":"45.614324278005938"},{"DAUID":"24640198","nbvisitors":"5","longitude":"-73.69798080966396","latitude":"45.717128360196966"},{"DAUID":"24663434","nbvisitors":"5","longitude":"-73.574534965366126","latitude":"45.4894264593454"},{"DAUID":"24663120","nbvisitors":"5","longitude":"-73.948554611068985","latitude":"45.406642321361936"},{"DAUID":"24490137","nbvisitors":"5","longitude":"-72.539540694503245","latitude":"45.9086501375339"},{"DAUID":"24661397","nbvisitors":"5","longitude":"-73.609805800126836","latitude":"45.515693042999608"},{"DAUID":"24290072","nbvisitors":"5","longitude":"-70.6713468499011","latitude":"46.1338224450566"}] But i need

[
[1025,35200855,2019,-79.3821,43.6486,1],
[1025,35570352,2019,-82.7175,46.3916,1],
[1025,35530299,2019,-81.1016,46.6009,1],
[1025,35560281,2020,-81.5358,48.3433,1],
[1025,35530243,2019,-80.9834,46.5063,1],
[1025,35570380,2019,-84.4283,47.6655,1],
[1025,35570342,2019,-82.6412,46.2302,1],
[1025,35230478,2020,-80.8498,43.8323,1],
[1025,35530270,2019,-80.9531,46.6552,1],
[1025,35280226,2019,-79.8006,42.8618,1],
[1025,35201576,2019,-79.5355,43.5952,1],
[1025,60010243,2020,-135.063,60.7208,1],
[1025,35230167,2019,-80.2255,43.5355,1],
[1025,35520100,2020,-82.1531,46.1918,1]

]

It is possible or not ?

doberkofler
  • 9,511
  • 18
  • 74
  • 126

2 Answers2

0

There are multiple libraries available in npm. You can try this package : https://www.npmjs.com/package/csv-grape

CsvGrape.getCsvData(event).then((_response)=>{
      this.data=_response;
      
    },(reject)=>{
      //log
    });
Nihar Sarkar
  • 1,187
  • 1
  • 13
  • 26
-1

All you need, is to install and use an existing csv parser like csv-parse

const parse = require('csv-parse')
const assert = require('assert')

const input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"'
parse(input, {
  comment: '#'
}, function(err, output){
  assert.deepStrictEqual(
    output,
    [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]
  )
})
doberkofler
  • 9,511
  • 18
  • 74
  • 126