# Extract data from String

**URL:** <https://discourse.nodered.org/t/extract-data-from-string/29430>\
**Category:** General\
**Created:** [3 July 2020 09:51 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430 "2020-07-03T09:51:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 09:51 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/1 "2020-07-03T09:51:40Z")

</div>

Hi All

Can someone please help me to write a function to do the following.  
Whenever there is a "confirmed" in the following string it needs to send out a msg.payload with that ID number that corresponds with the confirmed:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/5107fde6a4ab0a661378a9a9e52267ff4270cd15.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 July 2020 09:58 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/2 "2020-07-03T09:58:52Z")

</div>

I would help if you posted the actual message rather than having to guess (speaking for myself) the exact structure of the message.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 July 2020 10:06 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/3 "2020-07-03T10:06:18Z")

</div>

Where is the msg coming from?

It looks like it is a string but contains an object, however the objects are not seperated by commas, there are three objects so they need to be in an array, and the 'reservation\_state' is returning a boolean and a string.

If you have control of how the data is being sent, you should clean those issues up first.

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 10:11 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/4 "2020-07-03T10:11:31Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/94d530931e08aab1f2469673c01e4e9b8ff1888a.png)

---

<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:** [3 July 2020 10:13 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/5 "2020-07-03T10:13:02Z")

</div>

Since you have not said what generated this string I will tell you how you can make sense of this data - BUT - as @zenofmud rightly says, if you have control of this data, you should make it valid JSON then this becomes a very simple problem to solve.

So you could split on new line, replace single quotes with double quotes, replace `False` with `false` and `True` with `true`, surround it with square brackets then feed it through a JSON node.

Edit...  
Is this related to the odoo stuff?

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 10:42 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/6 "2020-07-03T10:42:34Z")

</div>

Yes it is to do with odoo.

Here is my flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/342033092dd3a7c5f4ae71c9a0156269ce44991a.png)

And my output:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da3c93a1a2b2085bdbe674131db96b5c6237bf66.png)

So lastly i need to pull that id number from one that is confirmed

---

<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:** [3 July 2020 11:20 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/7 "2020-07-03T11:20:59Z")

</div>

To test whether you have a message with confirmed, then in a function node you can do

```auto
if (msg.payload.reservation_state === "confirmed") {
  msg.payload = msg.payload.id
  return msg
}

```

Have a look at the node red docs page Working with Messages, there are many useful hints in there.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 July 2020 11:25 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/8 "2020-07-03T11:25:27Z")

</div>

You could also do it with a `switch` node that test msg.payload.reservation\_state followed by a `change` node to set msg.payload to msg.payload.id

Node-RED is flexible and there are different ways to solve most everything 😁

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 12:14 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/9 "2020-07-03T12:14:42Z")

</div>

Thank you Steve for all the help.

Almost done with the coding. looks like one more obstacle.  
After one more Jason node i receive this output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27af552e06a91220676f6b2978a1b5180e95f5f6.png)

If there is 2 or more orders, lets say maximum 5. How can I send each order (msg.payload) to a seperate S7 PLC node. So after the JSON node it must count how many msg.payloads there are and then send each one to a different S7 plc node where every PLC node will have a different DB address

---

<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:** [3 July 2020 12:20 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/10 "2020-07-03T12:20:47Z")

</div>

that depends on what your criteria is.

What determines which msg goes to which node?

Personally, where this data is generated, I would include a `.topic` value so it becomes a simple case of using a `switch` node to send it out of pin 1 for topic `x` , out of pin 2 for topic `y` etc etc.

source data (include a relevant topic) --\> node-red (convert to JS object) --\> switch node (route to an output based on value of topic)

If setting a topic in the source data is not an option - what in the msg tells you which PLC node to route to?

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 12:24 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/11 "2020-07-03T12:24:14Z")

</div>

Yeah i also thought of that but as the orders comes in the ID number will always increase.. When the order is confirmed it does not automatically goes to node red and then the PLC. I will have a request button on the HMI which will inject the pythonshell node and then retrieve all the orders which is confirmed. So I will confirm 2 orders on the PC now and then goes to the HMI and request it there. I have no clue how i can write topics to lets say that 2 orders. And the values received from odoo like the ID numbers and names will always be different....

---

<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:** [17 July 2020 12:36 UTC](https://discourse.nodered.org/t/extract-data-from-string/29430/12 "2020-07-17T12:36:40Z")

</div>

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