How to change strings into integer from an object

This is my msg.payload Object:

How could i change all string values into integers?

I think a function like this will work...

msg.payload = {"temperature":"35.1", "humidity":"27.3", "location": "Las Vegas"}
for (const key in msg.payload) {
    if (parseInt(msg.payload[key])) {
        msg.payload[key] = parseInt(msg.payload[key]);
    }  
}
return msg;

But I see that your example NewATURVendor is almost numeric (Hex?) and you might want to perform parseInt() only on selected fields.

I think that may need to be a bit more sophisticated. If the string value is "0" then the if will fail. Perhaps it should be a NaN test.

Thanks a lot. Here is my complete solution:

msg.measurement = "ZOOMsoft/DSL"

for ( const key in msg.payload ) {
    if ( typeof msg.payload[ key ] == "undefined" ) {
        msg.payload[ key ] = 0;
        node.warn(key);

    } else {
        if ( parseInt( msg.payload[ key ] ) ) {
            msg.payload[ key ] = parseInt( msg.payload[ key ] );
        }
    
    }

}


msg.payload = [ msg.payload , { name: "Banu Haus" } ];

return msg;

after that i send the data to an influxdb

But sometimes i get errors:

If typeof msg.payload[key] == "undefined" is not correct?

Add two named debug node, showing what is going into the function and what is coming out. What do they show when the write fails?

Period=undefined

That might be the spot where it fails.

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