{"action":"created","issue":{"url":"https://api.github.com/repos/zemirco/json2csv/issues/332","repository_url":"https://api.github.com/repos/zemirco/json2csv","labels_url":"https://api.github.com/repos/zemirco/json2csv/issues/332/labels{/name}","comments_url":"https://api.github.com/repos/zemirco/json2csv/issues/332/comments","events_url":"https://api.github.com/repos/zemirco/json2csv/issues/332/events","html_url":"https://github.com/zemirco/json2csv/issues/332","id":376992716,"node_id":"MDU6SXNzdWUzNzY5OTI3MTY=","number":332,"title":"Appending data to existing CSV file","user":{"login":"willbattel","id":13490339,"node_id":"MDQ6VXNlcjEzNDkwMzM5","avatar_url":"https://avatars1.githubusercontent.com/u/13490339?v=4","gravatar_id":"","url":"https://api.github.com/users/willbattel","html_url":"https://github.com/willbattel","followers_url":"https://api.github.com/users/willbattel/followers","following_url":"https://api.github.com/users/willbattel/following{/other_user}","gists_url":"https://api.github.com/users/willbattel/gists{/gist_id}","starred_url":"https://api.github.com/users/willbattel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/willbattel/subscriptions","organizations_url":"https://api.github.com/users/willbattel/orgs","repos_url":"https://api.github.com/users/willbattel/repos","events_url":"https://api.github.com/users/willbattel/events{/privacy}","received_events_url":"https://api.github.com/users/willbattel/received_events","type":"User","site_admin":false},"labels":[{"id":4833637,"node_id":"MDU6TGFiZWw0ODMzNjM3","url":"https://api.github.com/repos/zemirco/json2csv/labels/question","name":"question","color":"cc317c","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-11-02T22:57:57Z","updated_at":"2018-11-04T20:56:03Z","closed_at":"2018-11-04T20:56:03Z","author_association":"NONE","body":"Hi there.\r\n\r\nI'm trying to use `json2csv` in my JavaScript codebase (Node 8). I have an existing CSV file that I want to append more data to. The data exists only in memory (no JSON file) so I opted for `Parser` rather than `Transform`. The schema doesn't change, I just need to add more rows to the file from data in memory.\r\n\r\nI found the existing issue, #130, but this was closed without resolution and is over 2 years old. @knownasilya linked PR #128 as a solution but that was also closed without being merged.\r\n\r\nIs there no way to do this in JavaScript? The instructions for appending to CSV in the README are only for the CLI.\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/zemirco/json2csv/issues/comments/435705121","html_url":"https://github.com/zemirco/json2csv/issues/332#issuecomment-435705121","issue_url":"https://api.github.com/repos/zemirco/json2csv/issues/332","id":435705121,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNTcwNTEyMQ==","user":{"login":"juanjoDiaz","id":3322485,"node_id":"MDQ6VXNlcjMzMjI0ODU=","avatar_url":"https://avatars0.githubusercontent.com/u/3322485?v=4","gravatar_id":"","url":"https://api.github.com/users/juanjoDiaz","html_url":"https://github.com/juanjoDiaz","followers_url":"https://api.github.com/users/juanjoDiaz/followers","following_url":"https://api.github.com/users/juanjoDiaz/following{/other_user}","gists_url":"https://api.github.com/users/juanjoDiaz/gists{/gist_id}","starred_url":"https://api.github.com/users/juanjoDiaz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/juanjoDiaz/subscriptions","organizations_url":"https://api.github.com/users/juanjoDiaz/orgs","repos_url":"https://api.github.com/users/juanjoDiaz/repos","events_url":"https://api.github.com/users/juanjoDiaz/events{/privacy}","received_events_url":"https://api.github.com/users/juanjoDiaz/received_events","type":"User","site_admin":false},"created_at":"2018-11-04T20:56:03Z","updated_at":"2018-11-04T20:56:03Z","author_association":"COLLABORATOR","body":"That's not really valid CSV since the last row has a different number of columns than the rest.\r\nBut you know your need better than I do 🙂 \r\n\r\n`eol` won't help you here since it will include the `,` after every line including the last one.\r\n\r\nYour only option is to use post-processing:\r\n```js\r\nconst fs = require('fs');\r\n\r\n...\r\n\r\nconst csv = json2csv.Parse(jsonData);\r\nconst csvWithEndingCommas = csv.replace(/\\n/g, ',\\n');\r\n// Also you'd need to check if the file is empty or not to add the ',' or not on the next line\r\nfs.appendFile('filename.csv',  ',' + csvWithEndingCommas, (err) => {\r\n  if (err) console.error('Couldn\\'t append the data');\r\n  console.log('The data was appended to file!');\r\n});\r\n```\r\n\r\nBut really, I'd reconsider whatever you are trying to do 🙂 \r\n\r\nI'll close this since it has nothing to do with json2csv.\r\nBut feel free to keep the questions coming and I'll be happy to help."}}
{"actor":{"display_login":"juanjoDiaz"}}