Oye, necesito ayuda aquí. Quiero estructurar un archivo JSON. Aquí abajo puedo explicarlo un poco mejor:
Tengo un channelId y un serverName y quiero guardarlos en un archivo de configuración con fs. Así es como se vería ahora:
{ "server": "MyServer" }Pero quiero que se vea así:
[ { "channelId": "MyChannel", "info": [ { "server": "MyServer" } ] } ]Aquí está mi código:
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));Con "servidor" me refiero al servidor donde existe el canal. Gracias por ayudarme ;-)
Hay muchas maneras de hacer esto. Pruebe esto como una solución fácil.
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)); Más adelante, puede usar una función de map o bucle for en la matriz de plantillas para agregar canales y servidores dinámicamente.