Json lookup table:

Hey guys!

I need some help with this javascript function. You can see in the first picture that I have a payload with an: id, timestamp and value. But I want to change only the id to a better name then "104". In the second picture u see the payload that I want.

afbeelding ===> afbeelding

To change from "104" to "Battery_Current" I have to use a Json payload where i can vind the correspnding name to the number.

I have wirten this code but ofcourse it doesn't work because I have no idea how to write something like this.
What does the code need to do: The payload comes in and stores the id ("104") then it looksup in the TMPmetaTable where id "104" is and then it gives me the corresponding namen. After it found the name the program replaces the current id "104" with the new "Battery_Current".

var TMPmetaTable = 
{"Metadata":
{"0":{"name":"drive0_CurrentRPM","id":"101"},
"1":{"name":"drive1_CurrentRPM","id":"102"},
"2":{"name":"Battery_Voltage","id":"103"},
"3":{"name":"Battery_Current","id":"104"}}
};

var dataId = msg.payload.id;
var val = TMPmetaTable.Metadata.indexOf(dataID);


msg.payload.id = TMPmetaTable.Metadata[val].name;


return msg;

Thank you for helping and kind regards,
AxelD

You can replace the payload.id in a change node using following jsonata expression.

/* returns the name from the Metadata table for the id corresponding to the payload.id */
(Metadata~>$spread())[*.id=$$.payload.id].*.name

The jsonata expression assumes that the mapping table is provided in msg.Metadata.

1 Like

Hi AxelD,

i think it would be easier to make your lookup table into an array in order to use the find js command on it. Array.prototype.find()

var lookup = [
{"name":"drive0_CurrentRPM","id":"101"},
{"name":"drive1_CurrentRPM","id":"102"},
{"name":"Battery_Voltage","id":"103"},
{"name":"Battery_Current","id":"104"}
];

var lookupMatch = lookup.find(el => msg.payload.id === el.id);
node.warn(lookupMatch)

if(lookupMatch) {
    msg.payload.id = lookupMatch.name;
}

return msg;

Example flow:

[{"id":"467d10f5.83222","type":"inject","z":"1e053351.87668d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"id\":\"104\",\"qc\":0,\"qx\":20,\"ts\":\"2021-04-29T12:48:67.694Z\",\"val\":-10.199999809265137}","payloadType":"json","x":240,"y":400,"wires":[["e49ff8a2.733108","fe4555f9.48244"]]},{"id":"e49ff8a2.733108","type":"debug","z":"1e053351.87668d","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":320,"wires":[]},{"id":"fe4555f9.48244","type":"function","z":"1e053351.87668d","name":"","func":"var lookup = [\n{\"name\":\"drive0_CurrentRPM\",\"id\":\"101\"},\n{\"name\":\"drive1_CurrentRPM\",\"id\":\"102\"},\n{\"name\":\"Battery_Voltage\",\"id\":\"103\"},\n{\"name\":\"Battery_Current\",\"id\":\"104\"}\n];\n\nvar lookupMatch = lookup.find(el => msg.payload.id === el.id);\nnode.warn(lookupMatch)\n\nif(lookupMatch) {\n    msg.payload.id = lookupMatch.name;\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":400,"wires":[["4ccf3900.41f748"]]},{"id":"4ccf3900.41f748","type":"debug","z":"1e053351.87668d","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":400,"wires":[]}]

@UnborN
This works very good!
But I keep getting this warning message?

afbeelding

afbeelding

Thats due to the node.warn(lookupMatch) line .. that is there simply for debugging.
It shows in debug tab if there was a lookup match.

If you are sure about the code .. simple comment it out or delete it :wink:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.