I am trying to read a JSON file and then change 2 of the 3 values within it so that the last one does not change at all, my code is below, apparently I must add some more details so I am just going to try to fill this up a little bit with some text so you can probably stop reading at this point and start to evaluate my code, dont go too hard on me though I dont know that much Javascript and have been using it as my coding language for around 3 weeks mainly using documentation to aid me.
const fs = require('fs-extra');
const config_json = __dirname + "\\config.json"
const args = require('minimist')(process.argv.slice(2))
var exp_clean = args["clean"]
var exp_asked = args["asked"]
var remove_arg = args["remove"]
if (remove_arg == "") {
fs.readFile(config_json, 'utf8', (err, data) => {
if (err) {console.log("error", err)}
var obj = JSON.stringify(data, null, 4)
// how do i change values within the read json data?
// {
// "experimental-cleaning": "false",
// "experimental-asked": "false",
// "to-remove": ""
// }
// that is whats in the json file being read
})
var obj = {
"experimental-cleaning": exp_clean,
"experimental-asked": exp_asked,
"to-remove": to_remove
}
var stringified = JSON.stringify(obj, null, 4)
fs.writeFile(config_json, stringified, (err) => {
if (err) {console.log("error", err)}
})
}
else {var remove_arg}
var obj = {
"experimental-cleaning": exp_clean,
"experimental-asked": exp_asked,
"to-remove": to_remove
}
var stringified = JSON.stringify(obj, null, 4)
fs.writeFile(config_json, stringified, (err) => {
if (err) {console.log("error", err)}
})
Here is the fixed code
const fs = require('fs-extra');
const config = require('.\\config.json')
const args = require('minimist')(process.argv.slice(2))
const exp_clean = `${args["clean"]}`
const exp_asked = `${args["asked"]}`
const remove = `${args["remove"]}`
if (remove == 'undefined') {
config['experimental-cleaning'] = exp_clean
config['experimental-asked'] = exp_asked
config_string = JSON.stringify(config, null, 4)
fs.writeFile(__dirname + "\\config.json", config_string, (err) => {
if (err) {console.log("error"), err}
})
console.log("config updated")
}
else {
config['experimental-cleaning'] = exp_clean
config['experimental-asked'] = exp_asked
config['to-remove'] = remove
config_string = JSON.stringify(config, null, 4)
fs.writeFile(__dirname + "\\config.json", config_string, (err) => {
if (err) {console.log("error"), err}
})
console.log("config updated")
}