0

I have a txt file contains:

{"date":"2013/06/26","statement":"insert","nombre":1} {"date":"2013/06/26","statement":"insert","nombre":1} {"date":"2013/06/26","statement":"select","nombre":4}

how I can convert the contents of the text file as array such as:

statement = [
{"date":"2013/06/26","statement":"insert","nombre":1}, {"date":"2013/06/26","statement":"insert","nombre":1}, {"date":"2013/06/26","statement":"select","nombre":4}, ];

I use the fs module node js. Thanks

Sorry I will explain more detailed:

I have an array :

st = [
    {"date":"2013/06/26","statement":"insert","nombre":1},
    {"date":"2013/06/26","statement":"insert","nombre":5},
    {"date":"2013/06/26","statement":"select","nombre":4},
];

if I use this code :

var arr = new LINQ(st)
    .OrderBy(function(x) {return x.nombre;})
    .Select(function(x) {return x.statement;})
    .ToArray();

I get the result I want.

insert select insert

but the problem my data is in a text file. any suggestion and thanks again.

4
  • what data is in what text file? You're being to ambiguous here. Commented Jun 27, 2013 at 11:18
  • My file content : {"date":"2013/06/26","statement":"insert","nombre":1} {"date":"2013/06/26","statement":"insert","nombre":1} .... Commented Jun 27, 2013 at 11:21
  • Your question is completely different now, but I think the answers you have can tell you how to get your data into an array from a text file. Commented Jun 27, 2013 at 11:24
  • Yes .. that's what we all explained.. how to create that st array based on that file Commented Jun 27, 2013 at 11:24

4 Answers 4

2

There is no reason for not to do your file parser yourself. This will work on any size of a file:

var fs = require('fs');

var fileStream = fs.createReadStream('file.txt');

var data = "";

fileStream.on('readable', function() {
  //this functions reads chunks of data and emits newLine event when \n is found
  data += fileStream.read();
  while( data.indexOf('\n') >= 0 ){
    fileStream.emit('newLine', data.substring(0,data.indexOf('\n')));
    data = data.substring(data.indexOf('\n')+1);
  }
});

fileStream.on('end', function() {
  //this functions sends to newLine event the last chunk of data and tells it
  //that the file has ended
  fileStream.emit('newLine', data , true);
});

var statement = [];

fileStream.on('newLine',function(line_of_text, end_of_file){
    //this is the code where you handle each line
    // line_of_text = string which contains one line
    // end_of_file = true if the end of file has been reached
    statement.push( JSON.parse(line_of_text) );
    if(end_of_file){
               console.dir(statement);
               //here you have your statement object ready
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

YW .. but how deoxxa pointed out , it's good to put the JSON parser in a try{}catch{} so it won't crash from badly formatted JSON
Hi again, In my text file I have a empty line in the end so I get always this error : Unexpected end of input have u any suggestion for delete the last empty file and thanks.
I found it I add this condition : if(line_of_text != '') { statement.push( JSON.parse(line_of_text) ); }
as said ... you should just write try{statement.push( JSON.parse(line_of_text) );}catch(e){ } to avoid all errors that my appear at parsing
2

If it's a small file, you might get away with something like this:

// specifying the encoding means you don't have to do `.toString()`
var arrayOfThings = fs.readFileSync("./file", "utf8").trim().split(/[\r\n]+/g).map(function(line) {
  // this try/catch will make it so we just return null
  // for any lines that don't parse successfully, instead
  // of throwing an error.
  try {
    return JSON.parse(line);
  } catch (e) {
    return null;
  }
// this .filter() removes anything that didn't parse correctly
}).filter(function(object) {
  return !!object;
});

If it's larger, you might want to consider reading it in line-by-line using any one of the many modules on npm for consuming lines from a stream.

Wanna see how to do it with streams? Let's see how we do it with streams. This isn't a practical example, but it's fun anyway!

var stream = require("stream"),
    fs = require("fs");

var LineReader = function LineReader(options) {
  options = options || {};
  options.objectMode = true;

  stream.Transform.call(this, options);

  this._buffer = "";
};
LineReader.prototype = Object.create(stream.Transform.prototype, {constructor: {value: LineReader}});

LineReader.prototype._transform = function _transform(input, encoding, done) {
  if (Buffer.isBuffer(input)) {
    input = input.toString("utf8");
  }

  this._buffer += input;

  var lines = this._buffer.split(/[\r\n]+/);

  this._buffer = lines.pop();

  for (var i=0;i<lines.length;++i) {
    this.push(lines[i]);
  }

  return done();
};

LineReader.prototype._flush = function _flush(done) {
  if (this._buffer.length) {
    this.push(this._buffer);
  }

  return done();
};

var JSONParser = function JSONParser(options) {
  options = options || {};
  options.objectMode = true;

  stream.Transform.call(this, options);
};
JSONParser.prototype = Object.create(stream.Transform.prototype, {constructor: {value: JSONParser}});

JSONParser.prototype._transform = function _transform(input, encoding, done) {
  try {
    input = JSON.parse(input);
  } catch (e) {
    return done(e);
  }

  this.push(input);

  return done();
};

var Collector = function Collector(options) {
  options = options || {};
  options.objectMode = true;

  stream.Transform.call(this, options);

  this._entries = [];
};
Collector.prototype = Object.create(stream.Transform.prototype, {constructor: {value: Collector}});

Collector.prototype._transform = function _transform(input, encoding, done) {
  this._entries.push(input);

  return done();
};

Collector.prototype._flush = function _flush(done) {
  this.push(this._entries);

  return done();
};

fs.createReadStream("./file").pipe(new LineReader()).pipe(new JSONParser()).pipe(new Collector()).on("readable", function() {
  var results = this.read();

  console.log(results);
});

2 Comments

Really? -1? A description of what's wrong with my answer would be nice.
nice stream example ... but from practice if you just want a file reader it's better to use events. It goes faster. But the streams solution is good for reuse of code.
0
fs.readFileSync("myfile.txt").toString().split(/[\r\n]/)

This gets your each line as a string

You can then use UnderscoreJS or your own for loop to apply the JSON.parse("your json string") method to each element of the array.

Comments

0

var arr = fs.readFileSync('mytxtfile', 'utf-8').split('\n')

I think this is the simplest way of creating an array from your text file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.