2

Here is a sample of the .log file I need to convert. I am using Node.

    {"test": "data", "test1": 123, "foo": "feel me??"}
    {"test": "data", "test1": 123, "foo": "feel me??"}
    {"test": "data", "test1": 123, "foo": "feel me??"}

I am importing it by using this code.

let data = fs.readFileSync(log_path, 'utf8', function(err, data){
  if (err) throw err;
  let tweets = data.split('\n').map(line => JSON.parse(line));

  return tweets;

  fs.close(data, (err) => {
    console.log(err);
  })
})

As you can see it's not separated by commas so is not in JSON format. I am trying to read the file, then split it by a new line, but that doesn't seem to be working.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Mark
  • 1,610
  • 1
  • 14
  • 27

2 Answers2

3

Assuming "feel me??" is meant to be a property, you could split up the lines and then map them to an array of objects:

const text = `    {"test": "data", "test1": 123, "foo": "feel me??"}
    {"test": "data", "test1": 123, "foo": "feel me??"}
    {"test": "data", "test1": 123, "foo": "feel me??"}`;
const arrOfObjs = text.split('\n')
  .map(line => JSON.parse(line));
console.log(arrOfObjs);

The other problem is that readFileSync, as its name implies, reads the file synchronously. It doesn't accept a callback like that. Change your file-reading code to:

let data = fs.readFileSync(log_path, 'utf8');
// do stuff with the `data` string

Remember that since you're not working with a stream, you don't need fs.close.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Personally I used the read-last-lines package to get only a few last lines of my log file.

To make it work I had to slightly modify the code from the accepted answer, which was a great help for myself. I am going to add it here in case if someone was struggling with a similar issue.

readLastLines.read('info.log', 10)
      .then((lines) => {
        const arrOfStringObjs = lines.split('\n')
        let arrOfObjs = []
        arrOfStringObjs.forEach(strObj => {
          if (strObj !== undefined && strObj !== null && strObj !== '') {
            arrOfObjs.push(JSON.parse(strObj))
          }
        });
        console.log(arrOfObjs)

Hope it helps.

Jakub A Suplicki
  • 4,586
  • 1
  • 23
  • 31