Accessing msg properties that have a non standard variable identifier

I'm running into a problem with a function block to create fields for an InfluxDB connector. I use the JSON string to object conversion node in Node-RED and it correctly creates the object {"CF1":1,"CF2.5":1,"CF10":1,"PM1":1,"PM2.5":1,"PM10":1,"PB0.3":348,"PB0.5":89,"PB1":8,"PB2.5":0,"PB5":0,"PB10":0} which I then use as input to the function block below. The error is "a leading decimal point can be confused with a dot". I can see how this could be a problem but have no idea to pick up these values correctly. Any idea?

msg.payload = {
    cf1: msg.PMS5003.cf1,
    cf2.5: msg.PMS5003.cf2.5, <error
    cf10: msg.PMS5003.cf10,
    pm1: msg.PMS5003.pm1,
    pm2.5: msg.PMS5003.pm2.5, <error
    pm10: msg.PMS5003.pm10,
    pb0.3: msg.PMS5003.pb0.3, <error
    pb0.5: msg.PMS5003.pb0.5,
    pb1: msg.PMS5003.pb1,
    pb2.5: msg.PMS5003.pb2.5, <error
    pb5: msg.PMS5003.pb5,
    pb10: msg.PMS5003.pb10
}
return msg;

Hi firstly, in order to make code more readable and importable it is important to post it between two sets of three backticks - ``` - see this post for more details - How to share code or flow json

As for your issue, use msg.PMS5003["pm2.5"] when the variable contains non valid variable name charcaters.

PS, this is called bracket notation

1 Like

Thanks Steve on both fronts. That did the trick. You probably can tell that I'm at the deep end of the learning curve :slight_smile:

1 Like

If you are generating that data then it might be better to use underscore rather than dot, or just leave it out completely if that still gives you a unique name. Otherwise you will be forever cursing yourself in the future for making life harder. :slight_smile:

1 Like

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