Extract data from String

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:

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

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.

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.

Is this related to the odoo stuff?

1 Like

Yes it is to do with odoo.

Here is my flow:

And my output:

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

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

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.

1 Like

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 :grin:

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:

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

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?

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....

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