Am trying to update the key, value pair of a JSON file in protractor nodejs.Am unable to pass the 'key' param to the left-hand side of the 'equal to' operator in order to update the value in JSON file. Please find the below code and help me in resolving this :
Calling Method :
this.test = async function () {
await writeToJSON(title, test-name-07282020-1234);
}
Method:
async function writeToJSON(key, value) {
const { readFile, writeFile } = require('fs').promises;
const jsonFilePath = `${__dirname}/testdata.json`;
let data = JSON.parse(await readFile(jsonFilePath));
data.LANG.TEST2.Default.key = value; // line 5 ..due to key is not read from
// the parameter passed in the method. It is trying to look the field 'key' in
//json file to update the value.
writeFile(jsonFilePath, JSON.stringify(data, 2, 2)).then(() => {
console.log('Finished writing to json file')
})
}
testdata.json:
{
"LANG": {
"TEST1": {
"Default": {
"username": "value1",
"password": "value2"
}
},
"TEST2": {
"Default": {
"username": "value1",
"password": "value2",
"title": "test-name-05252020-4786"
}
}
}
It's failing at line 5
I want the key to read as 'title' and assign it the value 'test-name-07282020-1234'.Please help here!