I have object in the format
{
"room_1": {
"fan": "on",
"fan_state": 0,
"light1": "on",
"light2": "on",
"light3": "off",
"light4": "on"
},
"room_2": {
"fan": "on",
"fan_state": 2,
"light1": "off",
"light2": "on",
"light3": "on",
"light4": "off"
},
"room_3": {
"fan": "on",
"fan_state": 2,
"light1": "off",
"light2": "on",
"light3": "on",
"light4": "off"
},
"room_4": {
"fan": "on",
"fan_state": 3,
"light1": "off",
"light2": "off",
"light3": "off",
"light4": "on"
}
}
I want to update the value based on the incoming data which is of the (string)format and
{"room_1":{"light1":"off"}}
But i am not able to update it the full object with the updated values what i have tried is to find the key and update it but it wont work
[edit added code]
var jsonRequest=flow.get('post_msg') || {};
var jsonObj = JSON.parse(jsonRequest);
//var jsonObj = jsonRequest;
var js_keys = Object.keys(jsonObj);
//node.warn(jsonRequest);
node.warn("jsonObj " + jsonObj + " and " + Object.keys(jsonObj) + " type " + typeof (jsonObj));
var oldJSON= msg.payload;
var newJSON = oldJSON;
node.error("1 oldJSON_ payload_newjson " + JSON.stringify(oldJSON) );
for (var key in newJSON){
node.error("for ok: " + key + " " + JSON.stringify(jsonObj[key]) + "" + JSON.stringify(newJSON[key]) + " io " + Object.keys(JSON.parse(jsonObj)) + " val " + JSON.stringify(Object.values(JSON.parse(jsonObj)) ) );
if (key == Object.keys(jsonObj)+"" ){
node.warn("true");
}
node.error("1ppp " + JSON.stringify(newJSON[key]) );
for (var key2 in newJSON[key]){
node.error("2ppp " + jsonObj + " ok " + JSON.stringify(newJSON[key] ));
//if (key2 == Object.keys(JSON.parse(jsonObj) ) ) {
// newJSON[key][key2] = JSON.parse(jsonObj[key][key2]);
//}
}
//}
}
//newJSON.room_1.light1 ="off" ;
msg.payload=newJSON ;
var msg_keys = Object.keys(jsonObj);
node.error(msg_keys);
msg.room_no = msg_keys.toString();
msg.inputs =jsonRequest;
//msg.flags= oldJSON.flags;
return msg;
The code is not working too ..
Please help.