I have a json object as below. How can I change the value of all json keys by the values which is saved in file.
var
{
"name": "john",
"city": "CC"
}
I am not worried about the key here, just want to change the value sequentially according to the value given in file
File.txt
mark
MM
Below is the code which im trying
fs.readFile(path, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
//console.log(data);
var keys = Object.keys(obj);
keys.forEach(function (key) {
data.forEach(function(user){
key = user;
console.log(key);
});
})
});
Firstly, var is not allowed in JSON because it's reserved keyword. your JSON file should be:
{
"name": "john",
"city": "CC"
}
Your iterator function needs to be improved and use require function instead of low level fs API:
var obj = require("./your-data-file.json");
var keys = Object.keys(obj);
keys.forEach(function (key) {
console.log(obj[key]);
})