Object vs strings - extract a value from a split

Hi there, beginner warning!

I have this flow working fine to extract the humidity value from a string:

[{"id":"879a195a.741c98","type":"inject","z":"5996dc31.f79034","name":"{\"status\":\"ok\",\"humidity\":\"53\"}","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status\":\"ok\",\"humidity\":\"53\"}","payloadType":"str","x":460,"y":420,"wires":[["6e2e795d.9d47e8","715c958a.e0077c"]]},{"id":"715c958a.e0077c","type":"debug","z":"5996dc31.f79034","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":970,"y":420,"wires":[]},{"id":"6e2e795d.9d47e8","type":"function","z":"5996dc31.f79034","name":"Extract humidity","func":"var IN = msg.payload.split(\"\\\"\");\nvar HUMID = parseInt(IN[7]);\nvar msg1 = {payload:HUMID};\nreturn [msg1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":740,"y":480,"wires":[["715c958a.e0077c"]]}]

But the node giving me the values is outputting an object...

msg.payload : Object
{ status: "ok", humidity: "53"

I have no clue how to modify this code:

var IN = msg.payload.split(""");
var HUMID = parseInt(IN[7]);
var msg1 = {payload:HUMID};
return [msg1];

from an object.

Now I get this error:

function : (error)
"TypeError: msg.payload.split is not a function"

You need to understand the differnece (and advantage) of using objects and properties in node-red (and JavaScript in general).

For example, if you have an object, there is ZERO need to split strings etc.

JSON stands for JavaScript Object Notation - meaning it is a string representation of a JS object - which is useful for transmitting between devices and across the internet (they all do strings right!). That is why JSON is so popular - because it is REALLY EASILY convert to/from object/string.

So, when your data is a string, you have to split and mess about with it. Worse still if you want to change a value.


So the simple way to deal with JSON strings is to use a JSON node to convert the JSON string to a JS object. Or in you case, switch the inject to JSON mode ...

image

--- ↑ change to ↓ ---

image


then it is childs play grabing (or changing) any of the values you want...


I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

3 Likes

As Steve said you need to understand JSON and objects. Read his links and watch the videos.

Your inject feeds in a JSON string, while you say "the node givving me the value" is giving you an object. So i change your inject node to feed in an object.
You can then target/address the path to an object value. I show an example in the function node.

[{"id":"879a195a.741c98","type":"inject","z":"5a245aa1.510164","name":"{\"status\":\"ok\",\"humidity\":\"53\"}","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"status\":\"ok\",\"humidity\":\"53\"}","payloadType":"json","x":220,"y":2580,"wires":[["6e2e795d.9d47e8","715c958a.e0077c"]]},{"id":"6e2e795d.9d47e8","type":"function","z":"5a245aa1.510164","name":"Extract humidity","func":"msg.payload=parseInt(msg.payload.humidity);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":2640,"wires":[["715c958a.e0077c"]]},{"id":"715c958a.e0077c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":2580,"wires":[]}]

But definately try to get a understanding of JSON and objects and how to find the path to an object properties using the debug panel.

1 Like

This post should help to clarify Javascript objects and JSON.

1 Like

Wow! I'm impressed. Thanks!

Works like a charm! Thanks again


image

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