Reputation: 113
Here is what I'm trying to do in my Javascript File:
const file = require('../../data/files.json');
var data = {
"wolf": {"nani": "awoo"},
"loli": {"nani": "kimochi"}
}
file.push(data);
My current .json file contents:
{
"cat" : {
"nani": "meow"
},
"dog": {
"nani": "woof"
}
}
Here's kind of what I want my .json file to be after writing to the JSON file using NodeJS:
{
"cat" : {
"nani": "meow"
},
"dog": {
"nani": "woof"
},
"wolf": {
"nani": "awoo"
},
"loli": {
"nani": "kimochi"
}
}
Is this possible? If so thank you~!
Upvotes: 2
Views: 2899
Reputation: 11080
Yes, of course it's possible to modify JSON at runtime. However there are three problems with your current approach.
file.push(data)
- .push()
is a function on Arrays. However, file
is not an array, it is an object. You can use something like Object.assign(file, data)
to assign all the properties from data
to file
instead.
Your data
is in a different format than how you want your JSON result to look like. Instead of "wolf": "awoo"
it needs to be "wolf": { "nani": "awoo" }
, for example. You could keep it in the current format but you'd have to transform the data to get it in the right result format.
Modifying the JSON this way only modifies the copy of the JSON held in memory when it's read by require()
. You still need to save this modified copy of the JSON. Just like in a text editor where your changes aren't saved until you hit the save button. To do this you can use fs.writeFile
and JSON.stringify
:
//import the standard filesystem module to read/write files and folders
const fs = require("fs");
//after you modify your data use this to save the changes
fs.writeFile("../../data/files.json", JSON.stringify(file), err => {
if(err) console.log(err);
});
JSON.stringify
is needed because const file = require(...);
will convert the JSON to a JavaScript object. JSON is just text, a JS object is an in-memory data structure that has a prototype and properties and such things. JSON.stringify
converts it back to text so you can just save the text version of the object.
Unrelated to your question, but I see you're using discord.js, so this is probably for a discord bot. If you plan on modifying this JSON with a command, be wary that if your bot gets added to many servers, JSON storage gets very unstable and is likely in the future to lose/corrupt the data. This can happen for many reasons, but it's because every time you change the data it requires a full rewrite of the file, and if the process fails during the rewrite it is left in a corrupted state.
I strongly recommend using a proper database solution to store dynamic data, like postgres, redis, or any other SQL/NoSQL solution.
Upvotes: 2
Reputation: 143
You would have to read in the content of the file, add your changes and write out the file again.
What you did would result in this:
{
"cat" : {
"nani": "meow"
},
"dog": {
"nani": "woof"
}
}
{
"wolf": {
"nani": "awoo"
},
"loli": {
"nani": "kimochi"
}
}
You can't just append to JSON, you need to modify it. And to modify it you need to parse it.
Upvotes: 0
Reputation: 347
You can try like this
let file = require('../../data/files.json');
console.log(file);
// to add key value pair in any Object use like this :-
// object['key'] = 'value'
file['wolf'] = {
"nani": "awoo"
}
file['loli'] = {
"nani": "kimochi"
}
console.log(file);
Upvotes: 1