I am working on a project using node.js and am struggling to fit an JSON object at the right position into my already existing data. My file currently looks like this:
[
{
"id": "001",
"name": "Paul,
"city": "London"
},
{
"id": "002",
"name": "Peter,
"city": "New York"
},
...
]
I tried to arrange my data like:
var data = { id: id, name: name, city: city };
having the respective data stored in those variables. Then I used var json = JSON.stringify(data)
and tried
fs.appendFile("myJSONFile.json", json, function (err) {
if (err) throw err;
console.log('Changed!');
});
The file changes indeed but the new entry is positioned after the square bracket.
[
{
"id": "001",
"name": "Paul,
"city": "London"
},
{
"id": "002",
"name": "Peter,
"city": "New York"
},
...
]{"id":"004","name":"Mark","city":"Berlin"}
How do I get it alongside the previous entries? Any help would be truly appreciated!