I am creating a calendar program, and my aim is to save events and so on in a JSON file. What I think is the best way to do so is by storing arrays inside arrays in the JSON file (this way I can iterate through each array and load them when the program starts).
First and foremost: how do I push an array into an array in JSON? Any suggestions? Say for example I have variable var event
that is equal to an array {"id": elementTitle, "year": year, "month": month, "day": day};
that is going to be JSON.stringify(event)
'ed. So how do I save this to an array already created in a JSON file?: events = { }
.
By the way, this program is created with the electron api, also using node.js.
I am creating a calendar program, and my aim is to save events and so on in a JSON file. What I think is the best way to do so is by storing arrays inside arrays in the JSON file (this way I can iterate through each array and load them when the program starts).
First and foremost: how do I push an array into an array in JSON? Any suggestions? Say for example I have variable var event
that is equal to an array {"id": elementTitle, "year": year, "month": month, "day": day};
that is going to be JSON.stringify(event)
'ed. So how do I save this to an array already created in a JSON file?: events = { }
.
By the way, this program is created with the electron api, also using node.js.
Share Improve this question asked Mar 5, 2018 at 20:06 Frikk Ormestad LarsenFrikk Ormestad Larsen 4251 gold badge5 silver badges17 bronze badges 3- You can't because JSON is a string. You need to parse it, then modify, then stringify again. – Joost Meijer Commented Mar 5, 2018 at 20:10
- Oh, of course! Thank you very much ^^ – Frikk Ormestad Larsen Commented Mar 5, 2018 at 20:11
-
You’re confusing arrays (
[item, item, item...]
) and objects ({key:value, key:value, key:value...}
). – jcaron Commented Mar 5, 2018 at 20:12
2 Answers
Reset to default 1You could do something like this.
Declare events using [] signifying that it is an array.
//file.json
{
"events":[]
}
Using the node filesystem or "fs" module you can read and write to your file. The example below uses asynchronous reading and writing so the application will not stop and wait - but you can do this synchronously as well.
//app.js
let fs = require('fs');
fs.readFile('/path/to/local/file.json', 'utf8', function (err, data) {
if (err) {
console.log(err)
} else {
const file = JSON.parse(data);
file.events.push({"id": title1, "year": 2018, "month": 1, "day": 3});
file.events.push({"id": title2, "year": 2018, "month": 2, "day": 4});
const json = JSON.stringify(file);
fs.writeFile('/path/to/local/file.json', json, 'utf8', function(err){
if(err){
console.log(err);
} else {
//Everything went OK!
}});
}
});
More examples
I have variable var event that is equal to an array {"id": elementTitle, "year": year, "month": month, "day": day}
this isn't an array, it's an object
anyway, the way you'd modify a json that you saved to disk is by reading it from disk, JSON.parse
ing it into a javascript object(or array) modifying it and re-writting the file with the new object(or array)