3

How modified this function to add every object in file's new line?

   exports.addWaypoint = function(id, type, param){
        var dataIn = fs.readFileSync('./markers.json');
        var obj = JSON.parse(dataIn);          
        obj.markers.push({
            "id": id,
            "type": type,
            "param": param,
        });           
        writeJson(obj);    
    }
leftiness
  • 123
  • 7
user1824542
  • 225
  • 1
  • 4
  • 15
  • possible duplicate of [Node.js read and write file lines](http://stackoverflow.com/questions/11986350/node-js-read-and-write-file-lines) – leftiness Nov 19 '14 at 18:03

1 Answers1

2

This may be a duplicate of Javascript: How to generate formatted easy-to-read JSON straight from an object?

Here's a quick answer: use JSON.stringify with an optional parameter to indicate the indention for each nested element.

var o = {
    "id": 123,
    "type": "good",
    "param": { name: "Fred", age: 24 },
};

console.log( JSON.stringify(o,null,4) );
Community
  • 1
  • 1
Lee Jenkins
  • 2,299
  • 3
  • 24
  • 39