I have a file that has 6,000 of json. Its raw data and I need to add an comma ' , ' to the end of each line. Can someone help. Current code is creating a new line with a comma I need comma at end of each line. Node js or Javascript example is acceptable
The data file:
{"id": "67", "ac": []}
{"id": "78","ac": []}
{"id": "90", "ac": []}
What I am currently getting:
{"id": "67", "ac": []}
,
{"id": "78","ac": []}
,
{"id": "90", "ac": []}
What I need
{"id": "67", "ac": []},
{"id": "78","ac": []},
{"id": "90", "ac": []},
Current code
"use strict";
const fs = require("fs");
fs.readFile("users.json", (err, data) => {
if (err) throw err;
let student = data.toString();
var lines = student.split(/(\n|\r\n)/);
var new_content = lines
.map(function (line) {
return line + ",";
})
.join("\r\n");
fs.writeFile("newFile.json", new_content, (err) => {
if (err) throw err;
console.log("Data written to file");
});
Looking at your code, it looks like your trying to mutate a string by adding the comma in the map function. 'line' is immutable so you cant add the comma, but you can reassign the value; I.E line = line += ',' or newValue = line += ',' then return the value after that.