Heyo, I need help here. I want to structure a JSON file. Down here I can explain it a little bit better:
I have a channelId and a serverName and I want to safe these in a config file with fs. Here is how it would look now:
{
"server": "MyServer"
}
But I want it to look like this:
[
{
"channelId": "MyChannel",
"info": [
{
"server": "MyServer"
}
]
}
]
Here is my code:
const server = "MyServer"
const channel = "MyChannel" //I want this to be at the top
const template = { //this must be edited i think
server: "Cool Server",
}
template.server = server;
fs.writeFileSync("channels.json", JSON.stringify(template, null, 2));
With "server" I mean the server where the channel exists. Thanks for helping me ;-)
There are lots of ways you can do this. Try this as an easy fix.
const server = "MyServer";
const channel = "MyChannel"; //You want this to be at the top
const template = [
{
channelId: "Your Channel",
info: [
{
server: "Your Server",
},
],
},
];
template[0].channelId = channel;
template[0].info[0].server = server;
fs.writeFileSync("channels.json", JSON.stringify(template, null, 2));
Later on you can use a for loop or map function on the template array to add channels and servers dynamically.