3
\$\begingroup\$

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));
});
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 2, 2018 at 12:10
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
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.

answered Aug 9, 2018 at 10:49
\$\endgroup\$
3
  • \$\begingroup\$ Thank you so much Akshay for your solution I will give it a try. \$\endgroup\$ Commented 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\$ Commented Aug 10, 2018 at 21:23
  • \$\begingroup\$ You're welcome, Dave. Happy to help. \$\endgroup\$ Commented Aug 11, 2018 at 17:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.