how can I use the json object as a variable?
For example i want the age of sarah
function changeValue(jsonfile, object, value) {
const filepath = './' + jsonfile + '.json';
const data = JSON.parse(fs.readFileSync(filepath));
data.object = value;
fs.writeFileSync(filepath, JSON.stringify(object, null, 4));
console.log(data);
}
changeValue('file.json', data.users.sarah.age, '30');
//ReferenceError: data is not defined
file.json
{
"users": {
"adam": {
"age": "19",
"gender": "male"
},
"sarah": {
"age": "23",
"gender": "female"
},
}
}
Thanks in advance