0

So I'm having this code as a base and I have to use it to convert the content of "world_data.csv" to JSON.

I don't have a clue about how I can save that JSON in a variable. I guess the data I want is stored temporarly in "jsonArray", but how can I define a global variable which stores that data indefinetely?

var express = require('express');
var app = express();
var sys = require('util');
var path = require('path');
var bodyParser = require('body-parser');
var Converter = require("csvtojson").Converter;
app.use( bodyParser.json() );
app.use( express.static( path.join(__dirname, "public") ) );

var converter = new Converter({});

converter.on("end_parsed", function (jsonArray) {
    console.log(jsonArray);
});

require("fs").createReadStream("world_data.csv").pipe(converter);

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);
});
dun
  • 353
  • 1
  • 2
  • 18

2 Answers2

0

You need simply a global var. Plese read Javascript Closures

var express = require('express');
var app = express();
var sys = require('util');
var path = require('path');
var bodyParser = require('body-parser');
var Converter = require("csvtojson").Converter;
var jsonContent = {}; // <-- Your Global JSON Data
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));

var converter = new Converter({});

converter.on("end_parsed", function(jsonArray) {

    if (jsonArray) {

        jsonContent = jsonArray

    }

});

require("fs").createReadStream("world_data.csv").pipe(converter);

var server = app.listen(3000, function() {

    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s', host, port);

});
Community
  • 1
  • 1
Tim S.
  • 391
  • 1
  • 5
  • Thank you very much, that what I searched for :) – dun Dec 05 '16 at 09:35
  • can you explain why do you add a callback to the converter and not to the stream? For me the event "end_parsed" never happens, while I can add a pipe to a writable stream and save perfectly normal json array into a file. – Ufos Sep 26 '18 at 17:06
0

Depends what you want with parsed result, for e.g pass it to the view:

app.get('/', function(req, res) {
  require("fs").createReadStream("./world_data.csv").pipe(converter);
  converter.on("end_parsed", function (json) {
      res.json(json)
  });
});

As csvtojson provides api and hooks you can call whatever you need on parsing process finish.

Or you can use oneliner that can be implemented as:

app.get('/', function(req, res) {  
  require("fs").createReadStream("./world_data.csv").pipe(new Converter({constructResult:false})).pipe(res)
})
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
  • I don't have any clue, what you did there. I don't even understand my example really, because I nearly never use JS. – dun Dec 05 '16 at 09:35
  • What you want to do with parsed values? Pass it to to the front-end or to the some other function? Current version displays JSON when you open (get) the address `http://localhost:[port]/` – Krzysztof Safjanowski Dec 05 '16 at 10:52