Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
Express App Only Display JSON on Windows, Not Debian or Heroku

I have created an express app which successfully displays JSON on Windows - I have had no problem on Firefox or Chrome. However, when I try to run the same code on my linux setup (Debian) or deploy it on Heroku, receives an empty array: []. How can I get it to display on more than just windows? Here is the js file:

var express = require('express');
var router = express.Router();
var fs = require('fs');

//read file of chore todos
function readCSV(callback) {
  fs.readFile('./data/todoList.csv', function(err, data) {
    if(err) throw err;
    var dataStr = data.toString();
    var array = dataStr.split("\n");
    fs.writeFileSync('./data/todo.json', JSON.stringify(convertToJSON(array), null, 4));
    var temp = convertToJSON(array);
    return callback(temp);
  });
}

//convert array to JSON
function convertToJSON(array) {
  var rows = array.toString();
  rows = rows.split("\r");
  for (var i=1; i<rows.length;i++) {
    rows[i] = rows[i].replace(rows[i][0], "");
  }
  var asJSON = [];
  // get column headers
  var headers = rows[0].split(",");
  
  for (var i=1; i<rows.length-1; i++) {
    var obj = {}
    var currentRow = rows[i].split(",");

    for (var j=0; j<headers.length; j++) {
      obj[headers[j]] = currentRow[j];
    }
    asJSON.push(obj);
  }
  return asJSON;
}

var returnJSON;
readCSV(function(temp) {
  returnJSON=temp;
})

router.get('/', function (req, res, next) {
  res.json(returnJSON);
});

module.exports = router;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is caused by the below line in convertToJSON() function

rows = rows.split("\r");

Consider the following csv file created in windows.

enter image description here

And its binary representation: enter image description here

You will notice that the newline character is denoted by "\r \n" or CR LF characters

If the same csv is created in Linux, its binary version will look like below: enter image description here Here the newline character is denoted by "\n" or LF character.

Different operating systems handle the newline character differently. Refer https://en.wikipedia.org/wiki/Newline for more details.

To get it working for Linux, you can change to below

rows = rows.split("\n");

However, this will not work for windows.

How can I get it to display on more than just windows?

Use csvtojson to handle the edge cases like these and most of the heavy lifting. However if you want to handle it on your own you can check how the package handles it here

https://github.com/Keyang/node-csvtojson/blob/master/src/getEol.ts

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda