title: Read .json file line by line
body: <p>I have a json file with json objects in it, please how can I read the json objects line by line. Is this the best way to read a json file using node.js, pardon my question, just a node.js newbie</p>
  
  <p>My json file simply contains lines of json objects as:</p>
  
  <pre><code>file.json
  {"name":"user1"},
  {"name":"user2"},
  {"name":"user3"}
  </code></pre>
  
  <p>and I am trying to read the file with</p>
  
  <pre><code>app.get('/s',function(req,res){
    var fs = require('fs');
  var file = __dirname + '/file.json';
  
  
      fs.readFile(file, 'utf8', function (err, data) {
        if (err) {
          console.log('Error: ' + err);
          return;
        }
  
        data = JSON.parse(data);
  
        console.dir(data);
      });
      });
  </code></pre>
  
  <p>I get an error with that code that says "unexpected token {" just on the second line", tanks</p>
  
  <p>//EDIT
  It turned out my Json file was invalid because readFile reads the entire file, I changed it by putting the objects in an array:</p>
  
  <pre><code>file.json
  [
  {"name":"user1"},
  {"name":"user2"},
  {"name":"user3"}
  ]
  </code></pre>
  
  <p>Then this code reads the entire file (array)</p>
  
  <pre><code>var fs = require('fs');
  var file = __dirname + '/file.json';
  
  fs.readFile(file, 'utf8', function (err, data) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
  
    data = JSON.parse(data);
  
    data.forEach(function(user){ 
      console.log(user.name);
    })
  
  });
  </code></pre>