I am making a discord bot and I have a profile.json file and I want to be able to make it so when you do a certain command, it adds the argument you input to the array. like so
{"Profile_name":"id":"idhere", "array":["item_1"]}
I want to be able to add more items to that array when a user uses the command to do so
First, u have syntax error in your json.
Second, u ask for ready code.
Third, here, u have scrap that you can start with:
const fs = require("fs");
// read file
let my_json = fs.readFileSync(path_to_file);
my_json = JSON.parse(my_json);
// edit variable here
// for example
my_json.array.push("new item");
// write file
my_json = JSON.stringify(my_json);
fs.writeFileSync(path_to_file, my_json);
You need to read the file, push new element to array and then save the file
const fs = require('fs');
//reads the file
let rawdata = fs.readFileSync('file.json');
//convert to JSON object
let json = JSON.parse(rawdata);
//Before: {"Profile_name":"name","id":"idhere", "array":["item_1"]}
json["array"].push("more");
//After: {"Profile_name":"name","id":"idhere", "array":["item_1","more"]}
//Convert to string
let data = JSON.stringify(json);
//Save the file
fs.writeFileSync('file.json', data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.