Find and replace

I have two object,
obj1: {"16401":0,"16402":0,"16403":32768,"16404":0,"16405":0}
and obj2: {"0":{"Address":16401,"Name":"EE_Time_Cfg_i_TimeZoneOffset","val":0,"Default1":"60","Unit":"min","Description":"Przesuniecie czasu wzgledem UTC"},"1":{"Address":16402,"Name":"EE_Time_Cfg_x_DST_Enable","val":0,"Default1":"1","Unit":null,"Description":"Automatyczna zmiana czasu"},"2":{"Address":16403,"Name":"EE_Time_Cfg_x_SNTP_Enable","val":0,"Default1":"0","Unit":null,"Description":"Automatyczna aktualizacji czasu z internetu"},"3":{"Address":17313,"Name":"EE_Roller1_TimePulse","val":0,"Default1":"20","Unit":"s","Description":"Czas trwania impulsu roleta 1"},"4":{"Address":17314,"Name":"EE_Roller2_TimePulse","val":0,"Default1":"20","Unit":"s","Description":"Czas trwania impulsu roleta 2"}}

I want copy value from obj1 to obj2[i].val where obj1.key= obj2[i].Address
How to do that?

[{"id":"17d71502.d42523","type":"function","z":"a6893b09.aee2d8","name":"","func":"var obj1 = {\"16401\":0,\"16402\":0,\"16403\":32768,\"16404\":0,\"16405\":0};\nvar obj2 = {\"0\":{\"Address\":16401,\"Name\":\"EE_Time_Cfg_i_TimeZoneOffset\",\"val\":0,\"Default1\":\"60\",\"Unit\":\"min\",\"Description\":\"Przesuniecie czasu wzgledem UTC\"},\"1\":{\"Address\":16402,\"Name\":\"EE_Time_Cfg_x_DST_Enable\",\"val\":0,\"Default1\":\"1\",\"Unit\":null,\"Description\":\"Automatyczna zmiana czasu\"},\"2\":{\"Address\":16403,\"Name\":\"EE_Time_Cfg_x_SNTP_Enable\",\"val\":0,\"Default1\":\"0\",\"Unit\":null,\"Description\":\"Automatyczna aktualizacji czasu z internetu\"},\"3\":{\"Address\":17313,\"Name\":\"EE_Roller1_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 1\"},\"4\":{\"Address\":17314,\"Name\":\"EE_Roller2_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 2\"}};\n\nmsg.obj1 = obj1;\nmsg.obj2 = obj2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":680,"wires":[["85658fe5.e4f4e"]]},{"id":"2235f796.ec22d8","type":"inject","z":"a6893b09.aee2d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":680,"wires":[["17d71502.d42523"]]},{"id":"85658fe5.e4f4e","type":"debug","z":"a6893b09.aee2d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":600,"y":680,"wires":[]}]

you could do it in a change node with a JSONata expression

[{"id":"b5d346d1.5ba5d8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"original","v":"{\"0\":{\"Address\":16401,\"Name\":\"EE_Time_Cfg_i_TimeZoneOffset\",\"val\":0,\"Default1\":\"60\",\"Unit\":\"min\",\"Description\":\"Przesuniecie czasu wzgledem UTC\"},\"1\":{\"Address\":16402,\"Name\":\"EE_Time_Cfg_x_DST_Enable\",\"val\":0,\"Default1\":\"1\",\"Unit\":null,\"Description\":\"Automatyczna zmiana czasu\"},\"2\":{\"Address\":16403,\"Name\":\"EE_Time_Cfg_x_SNTP_Enable\",\"val\":0,\"Default1\":\"0\",\"Unit\":null,\"Description\":\"Automatyczna aktualizacji czasu z internetu\"},\"3\":{\"Address\":17313,\"Name\":\"EE_Roller1_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 1\"},\"4\":{\"Address\":17314,\"Name\":\"EE_Roller2_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 2\"}}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"16401\":0,\"16402\":0,\"16403\":32768,\"16404\":0,\"16405\":0}","payloadType":"json","x":100,"y":3720,"wires":[["ed37addf.5f5a8"]]},{"id":"ed37addf.5f5a8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"original.*.$merge([$,{\"val\":$lookup($$.payload,$string($.Address))}])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":3720,"wires":[["b60cc7a4.8993f8"]]},{"id":"b60cc7a4.8993f8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":3720,"wires":[]}]
original.*.$merge([$,{"val":$lookup($$.payload,$string($.Address))}])

the objects are in the inject node and named original and payload

1 Like

You could use a function node...

var obj1 = {"16401":0,"16402":0,"16403":32768,"16404":0,"16405":0};
var obj2 = {"0":{"Address":16401,"Name":"EE_Time_Cfg_i_TimeZoneOffset","val":0,"Default1":"60","Unit":"min","Description":"Przesuniecie czasu wzgledem UTC"},"1":{"Address":16402,"Name":"EE_Time_Cfg_x_DST_Enable","val":0,"Default1":"1","Unit":null,"Description":"Automatyczna zmiana czasu"},"2":{"Address":16403,"Name":"EE_Time_Cfg_x_SNTP_Enable","val":0,"Default1":"0","Unit":null,"Description":"Automatyczna aktualizacji czasu z internetu"},"3":{"Address":17313,"Name":"EE_Roller1_TimePulse","val":0,"Default1":"20","Unit":"s","Description":"Czas trwania impulsu roleta 1"},"4":{"Address":17314,"Name":"EE_Roller2_TimePulse","val":0,"Default1":"20","Unit":"s","Description":"Czas trwania impulsu roleta 2"}};

const keys = Object.keys(obj2);
for (let index = 0; index < keys.length; index++) {
    const key = keys[index];
    let o = obj2[key];
    let v = obj1[o.Address];
    if(v != null)  o.val = v;
}

msg.payload = obj2;
return msg;

image

Demo flow...

[{"id":"17d71502.d42523","type":"function","z":"8ac17474d97456ce","name":"","func":"var obj1 = {\"16401\":0,\"16402\":0,\"16403\":32768,\"16404\":0,\"16405\":0};\nvar obj2 = {\"0\":{\"Address\":16401,\"Name\":\"EE_Time_Cfg_i_TimeZoneOffset\",\"val\":0,\"Default1\":\"60\",\"Unit\":\"min\",\"Description\":\"Przesuniecie czasu wzgledem UTC\"},\"1\":{\"Address\":16402,\"Name\":\"EE_Time_Cfg_x_DST_Enable\",\"val\":0,\"Default1\":\"1\",\"Unit\":null,\"Description\":\"Automatyczna zmiana czasu\"},\"2\":{\"Address\":16403,\"Name\":\"EE_Time_Cfg_x_SNTP_Enable\",\"val\":0,\"Default1\":\"0\",\"Unit\":null,\"Description\":\"Automatyczna aktualizacji czasu z internetu\"},\"3\":{\"Address\":17313,\"Name\":\"EE_Roller1_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 1\"},\"4\":{\"Address\":17314,\"Name\":\"EE_Roller2_TimePulse\",\"val\":0,\"Default1\":\"20\",\"Unit\":\"s\",\"Description\":\"Czas trwania impulsu roleta 2\"}};\n\nconst keys = Object.keys(obj2);\nfor (let index = 0; index < keys.length; index++) {\n    const key = keys[index];\n    const o = obj2[key];\n    const v = obj1[o.Address];\n    if (v != null) o.val = v;\n}\n\nmsg.payload = obj2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":320,"wires":[["85658fe5.e4f4e"]]},{"id":"2235f796.ec22d8","type":"inject","z":"8ac17474d97456ce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":500,"y":320,"wires":[["17d71502.d42523"]]},{"id":"85658fe5.e4f4e","type":"debug","z":"8ac17474d97456ce","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":320,"wires":[]}]
1 Like

I don't understand JSonata and I couldn't immediately understand Steve-Mcl's function solution. I think they are experts, I'm not! So I wrote an alternative. It's definitely not efficient since it loops through the entire obj2 for each obj1.

for (var key1 in msg.obj1){
    for (var key2 in msg.obj2)
    {
        if (msg.obj2[key2]["Address"] == key1) {
            msg.obj2[key2]["val"] = msg.obj1[key1];
        } 
    }
}
return msg;
1 Like