# Object vs strings - extract a value from a split

**URL:** <https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112>\
**Category:** General\
**Created:** [5 March 2021 18:34 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112 "2021-03-05T18:34:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonFili](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonfili/32/33235_2.png) [@SimonFili](https://discourse.nodered.org/u/SimonFili)\
**Post date:** [5 March 2021 18:34 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/1 "2021-03-05T18:34:44Z")

</div>

Hi there, beginner warning!

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

```auto
[{"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"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 March 2021 18:45 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/2 "2021-03-05T18:45:47Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/186a3c505262be4a54c1824d5210934068136983.png)

--- ↑ change to ↓ ---

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5c8f7a70da5f1e7eea6868028f3c7c7feed51ca.png)

  

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

 ![lj2h5HBf8h](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f919d112155e8d7557ba849d740f3a5f3755e97b.gif)

* * *

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 March 2021 19:37 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/3 "2021-03-05T19:37:54Z")

</div>

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.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 March 2021 21:29 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/4 "2021-03-05T21:29:09Z")

</div>

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify Javascript objects and JSON.

---

<div class="post-metadata">

**Author:** ![SimonFili](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonfili/32/33235_2.png) [@SimonFili](https://discourse.nodered.org/u/SimonFili)\
**Post date:** [6 March 2021 00:15 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/5 "2021-03-06T00:15:08Z")

</div>

Wow! I'm impressed. Thanks!

---

<div class="post-metadata">

**Author:** ![SimonFili](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simonfili/32/33235_2.png) [@SimonFili](https://discourse.nodered.org/u/SimonFili)\
**Post date:** [6 March 2021 00:23 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/6 "2021-03-06T00:23:38Z")

</div>

Works like a charm! Thanks again

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c4132a914cfcc8987dece512aea9211baf201082.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8cfd2f6ba832e93968d952e420baae9b1609960b.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 March 2021 00:24 UTC](https://discourse.nodered.org/t/object-vs-strings-extract-a-value-from-a-split/42112/7 "2021-03-20T00:24:01Z")

</div>

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