I have the following Data.json file and I would like to update some of the values.
{
"A0_Water":"-306.3",
"A1_Water":"0.0",
"A2_Water":"0.0",
"A3_Water":"0.0",
"Barometer":"100.8",
"Bar_Offset":"0",
"Temp":"29.95",
"ip":"192.168.2.47",
"serial":"02:42:52:bf:82:27",
"A0_Sensor":"PS30A",
"A1_Sensor":"None",
"A2_Sensor":"None",
"A3_Sensor":"None",
"A0_Offset":"0",
"A1_Offset":"0",
"A2_Offset":"0",
"A3_Offset":"0"
}
I am new to coding so any help will be much appriciated. This is the code that I have working: The var data object that I created contains the entries that I want to update in the Data.json file but I was not sure how to use it. The remainder of the code is what I was able to get working but I am sure there is a better way to update the entries in the Data.json file
var data = {
'A0_Water': c_inh2o_A0,
'A1_Water': c_inh2o_A1,
'A2_Water': c_inh2o_A2,
'A3_Water': c_inh2o_A3,
'Barometer': b_mbar_corrected,
'Temp': b_temp
};
var fileName = '/home/admin/reactwatertracker/src/var/Data.json';
var file4 = require(fileName);
var file5 = file4;
var file6 = file4;
var file7 = file4;
var file8 = file4;
var file9 = file4;
file4['A0_Water'] = c_inh2o_A0;
file5['A1_Water'] = c_inh2o_A1;
file6['A2_Water'] = c_inh2o_A2;
file7['A3_Water'] = c_inh2o_A3;
file8['Temp'] = b_temp;
file9['Barometer'] = b_mbar_corrected;
console.log('Writing to' + fileName);
fs.writeFile(fileName, JSON.stringify(file4,file5,file6,file7,file8,file9, null, 2), function (err) {
if (err) return console.log(err);
console.log (JSON.stringify(file4));
});
1 Answer 1
const fs = require('fs');
// Read data from JSON file and parse it as a JSON object
const dataFromFile = JSON.parse(fs.readFileSync('./Data.json', 'utf8'));
const data = {
'A0_Water': 'c_inh2o_A0',
'A1_Water': 'c_inh2o_A1',
'A2_Water': 'c_inh2o_A2',
'A3_Water': 'c_inh2o_A3',
'Barometer': 'b_mbar_corrected',
'Temp': 'b_temp'
};
// Now for every 'property' in your 'data' object here,
// you need to update the 'dataFromFile' object
for(let property in data) {
if(data.hasOwnProperty(property)) {
dataFromFile[property] = data[property];
}
}
// Write the updated data in the JSON file
fs.writeFileSync('./Data.json', JSON.stringify(dataFromFile));
NOTE: The above code will add
a new value in the JSON file if a corresponding key
is not found in your data
object. You may want to add a check as:
for(let property in data) {
if(data.hasOwnProperty(property)) {
if(dataFromFile[property]) {
dataFromFile[property] = data[property];
}
}
}
Now, the update will be preformed only when a corresponding key has been found.
-
\$\begingroup\$ Thank you so much Akshay for your solution I will give it a try. \$\endgroup\$Dave Murphy– Dave Murphy2018年08月10日 00:25:39 +00:00Commented Aug 10, 2018 at 0:25
-
\$\begingroup\$ I have implemented your code and it is much cleaner than what I had and it allows me to clean up other parts of my code. Thank you for your help. \$\endgroup\$Dave Murphy– Dave Murphy2018年08月10日 21:23:27 +00:00Commented Aug 10, 2018 at 21:23
-
\$\begingroup\$ You're welcome, Dave. Happy to help. \$\endgroup\$Akshay Anurag– Akshay Anurag2018年08月11日 17:59:22 +00:00Commented Aug 11, 2018 at 17:59