Home > front end >  Node-Red: Parse JavaScript Object with Double value
Node-Red: Parse JavaScript Object with Double value

Time:07-23

I am using node-RED to call in data from a robot. In the debug window it says it is a 'msg: Object', and when I copy it to a notepad it takes the format: {"topic":"","payload":27.659992218017578,"_session":{"type":"tcp","id":"0151ff7339437ec6"},"_msgid":"6a6897605a523366"}

I am also not sure if this is a JSON object or not, as I see examples with '' around the brackets.

I am trying to use the function node within node-red to parse this to attain the "payload" value. However, it keeps returning as undefined.

I am using the script:

var json =msg.payload;
var obj = JSON.parse(json);
msg.payload = console.log(obj.payload);
return msg;

I am a beginner to javascript and JSON, however I have tried searching and all examples only have integers as the parsing value. I am also unsure if the value name itself 'payload' is causing an issue. I have also attempted to stringify and using 'getDouble' but had no luck, which I owe to my lack of experience.

I appreciate any guidance.

CodePudding user response:

You don't need to do anything. The value of msg.payload is already a double.

Without a lot more context of what you are trying to do there isn't anything else that we can say here.

CodePudding user response:

After the node that gets the above information (the data in {} in the question}, I then added the 'json' node, and then used the debug node with msg.payload as the output. This output the payload value above as a string. I have now been able to add a function node that allows me to edit the data as needed.

  • Related