0

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.

LaurenMcG
  • 11
  • 3
  • *"In the debug window it says it is a 'msg: Object'"* Then it's not JSON. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. `msg.payload` should be all you need to access that number: https://jsfiddle.net/tjcrowder/tgcvzk3h/ – T.J. Crowder Jul 21 '22 at 15:48
  • Related (at least): https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – T.J. Crowder Jul 21 '22 at 15:51
  • Thanks, I wasn;t overally sure as the way the data was when I copied it out to a notepad made the data look as pasted above. I have changed the function node to then be msg.payload=payload; return msg; however I am getting the error 'payload is not defined' – LaurenMcG Jul 21 '22 at 15:53
  • Why would you do that? The value is in `msg.payload`. As far as I know, there's no `payload` variable in your code (which is confirmed by the error). – T.J. Crowder Jul 21 '22 at 15:55
  • What is the purpose of the three lines above `return msg`? You don't seem to use `msg.payload` for anything, so...? – T.J. Crowder Jul 21 '22 at 15:57
  • Because I am trying to get this value out as a single value, away from the rest of the data. Sorry, I am unsure of how I get this out, hence why I thought the parse way was the right approach. – LaurenMcG Jul 21 '22 at 15:58
  • Let's step back: What do you see when you do `console.log(msg)`? (Or better yet, when you look at it in a debugger.) And what do you want to return? Just the number? – T.J. Crowder Jul 21 '22 at 15:58
  • If it type console.log(msg); return msg into the function node, the debugger just shows the same output if this was not done (the output is data in the {} above). – LaurenMcG Jul 21 '22 at 16:05
  • That doesn't seem to match with what you've said in the question. What do you get for `console.log(typeof msg)`? – T.J. Crowder Jul 21 '22 at 16:06
  • To clarify am I to do 'return console.log(typeof msg)' and then see what the response is in the debug window? If so, nothing it showing at all – LaurenMcG Jul 21 '22 at 16:11
  • No, I didn't say `return console.log(typeof msg)`, I said `console.log(typeof msg)`. But if you didn't see anything in the "debugging panel", then -- **bizarrely** -- the "debugging panel" must not show you `console.log` output. (But then, where did you see the previous stuff?) Sorry, I don't use Node-RED, so I'm going to have to give up at this point. Good luck. – T.J. Crowder Jul 21 '22 at 16:27

2 Answers2

1

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.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • I am looking to extract the float number itself so that I can append other words/formatting around it. This is because I am using it in this format to be inputted into an IoT Platform, and it requires inputs in a certain way. I can do the appending easily, I just need to get the float number out individually, and not in its pair. – LaurenMcG Jul 22 '22 at 08:36
  • Then you just reference it as `msg.payload`, build the new string with it, assign that new string back to `msg.payload` and `return msg` – hardillb Jul 22 '22 at 08:54
0

After the node that gets the above information (the data in {} in the question}, I then used the function node to construct the message that I wanted to sent to the IIOT platform. I did

const str = msg.payload
msg.payload = " ," + str
// where the text I required was in " "
return msg

Also this works:

msg.payload = ","  + msg.payload
return msg

And then I used the MQTT output node to publish this to the IIOT platform

LaurenMcG
  • 11
  • 3