Simplifying syntax of If..Else. in a function node

I want to conditionally set the value of a flow.context based on the msg.payload value

i am able to achieve the same with following, (see first three lines), but is there an easy way (less code/better code) I have a bunch of them to set.

PS: the value that is checked is actually a string "__null", i have used a slice function before this node

if (msg.M00==="__null"){
flow.set("M0009", msg.M00)};

flow.set("M0109", msg.M01);
flow.set("M0209", msg.M02);
flow.set("M0309", msg.M03);
flow.set("M0409", msg.M04);
flow.set("M0509", msg.M05);
flow.set("M0609", msg.M06);
flow.set("M0709", msg.M07);
flow.set("M0809", msg.M08);
flow.set("M0909", msg.M09);
flow.set("M1009", msg.M10);

Something like this (or at least for some inspiration)


for(let x=0;x<=10;x++){

    let m = x < 10 ? `M0${x}` : 'M10'
    if (msg[m] == "__null"){
        flow.set(`${m}09`, '_____0')
    } else {
        flow.set(`${m}09`, msg[m])


Would it not be easier/smarter rather than slicing them to store them as Objects in context store and then just reference each of the values ?

Obviously we would need to see the whole incoming message before you slice it - but i am sure one of the JSONATA gurus on here would be able to turn it into Context variables pretty quickly - particularly if you are going to have a few of them


1 Like

so I have different data come in from an analog to digital converter.
its pressure readings.
here is function code that I had help with.
this may get you in the correct direction if they are all coming in one message and you need to split and give each part its own message name.

let msg0 = {};
let msg1 = {};
let msg2 = {};
let msg3 = {};
//let msg4 = {};

msg0.payload = msg.payload.POWER1;
msg1.payload = msg.payload.POWER2;
msg2.payload = msg.payload.POWER3;
msg3.payload = msg.payload.POWER4;

msg0.topic = "POWER1"
msg1.topic = "POWER2"
msg2.topic = "POWER3"
msg3.topic = "POWER4"

return [msg0, msg1, msg2, msg3];

Would need to understand the incoming data to know if there is an easier way.

Here are two ways

for(let i = 0; i < 11; i++){
    let m = "M" + i.toString().padStart(2, "0");    
    let m9 = m + "09";
    if (msg[m] === "__null"){
        flow.set(m9, "_____0");
        flow.set(m9, msg[m])

untested, may be typo's

Or, assuming msg has no other properties

msg = {M00: "__null", M01: "test", _msgid: "wrttytw555"}; // for testing
let M_set = [];
let M_msg = [];
delete msg._msgid;
for(const[key,value] of Object.entries(msg)){
    M_set.push(key + "09");
    if(value === "__null"){

by slicing i mean to use the function slice to get exact number of characters in the output

var A00 = ("______" + msg.payload[0].s0).slice(-6);

the incoming data is an array by a MySql query output.


one such array for each hour, and then i store the values in flow.context for 8 hours and send out as a Shift Report (8 Hour period) for every hour.

the 'issue ' was that since each machine output has a different number of integers the report was not aligned for each hour and difficult to read,

so i added a slice function to get around that to have a visually cleaner report, that i send through mail. since the report template is fixed for 8 hours, and for the hour which is still not passed, i am getting 'null', to replace that null with a zero, i had used the if else statement, to simplify that code was the intention of my original post.

if there are no null values, the report just looks fine

will give it a try and report.

Thanks everyone. !

If all you want to do is replace "_____null" with "________0", would it be simpler just to do a
text_output = text_output.replace(/___null/g, "______0");
On the finished report text.

1 Like


there is always a simpler solution.

is there a better way to align the texts properly ? like a TAB character between words ?

I got the solution here. now the table looks much better. no need for slicing to get same number of digits!

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