javascriptnode.jsjsonnode.js-fs

How do I use the replacer function with JSON.stringify(...)?


I've been trying to use

const fs = require("fs");
const settings = require("./serversettings.json")
let reason = args.join(' ');
function replacer(key, value) {
    return reason;
}
fs.writeFileSync(settings, JSON.stringify(settings.logchannel, replacer))

It seems like to me that it doesn't work, so I'm trying to figure out how replacers work as MDN made me even more confused.


Solution

  • settings is an object, not a filename.

    I'm trying to replace the string in the settings named as "logchannel" to whatever I tell it to change it to

    const fs = require("fs");
    var settings = require("./serversettings.json");
    settings.logchannel = "foo"; //specify value of logchannel here
    fs.writeFileSync("./serversettings.json", JSON.stringify(settings));