Wednesday 17 May 2017

JSON File append Objects

I'm using a for loop to iterate over some links and want to save for each index

var file = './data/home.json'

_.forEach(unique, (data) => {
  var result = {
      longitude:  data.lng, 
      latitude:   data.lat
  }
  jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})
});

The problem is that the output does not create trailing commas nor is it enclosed inside an array. I'm using the flag: 'a' method to append objects on each for each loop.... Please help

{
  "longitude": 12.5893247949521,
  "latitude": 55.6768760704466
}
{
  "longitude": 12.5821935238857,
  "latitude": 55.6778938800944
}
{
  "longitude": 12.5905159440853,
  "latitude": 55.6803210250784
}



via Octavian L

No comments:

Post a Comment