A Node-RED flow works by passing messages between nodes. The messages are simple JavaScript objects that can have any set of properties.
Messages usually have a payload
property - this is the default property that
most nodes will work with.
Node-RED also adds a property called _msgid
- this is an identifier for
the message which can be used to trace its progress through a flow.
{
"_msgid": "12345",
"payload": "..."
}
The value of a property can be any valid JavaScript type, such as:
true
, false
0
, 123.4
"hello"
[1,2,3,4]
{ "a": 1, "b": 2}
More information about JavaScript types
The easiest way to understand the structure of a message is to pass it to a Debug node and view it in the Debug sidebar.
By default, the Debug node will display the msg.payload
property, but can be
configured to display any property or the whole message.
When displaying an Array or Object, the sidebar provides a structured view that can be used to explore the message.
msg.payload
has been used.Object
, String
, Array
etc.When you hover over any element, a set of buttons appear on the right:
: copies the
path to the selected element to your clipboard. In this example, it will copy
payload.Phone[2].type
. This allows you to quickly determine how to access a
property in a Change or Function node.
: copies the value of the element to your clipboard as a JSON string. Note that the sidebar truncates Arrays and Buffers over a certain length. Copying the value of such a property will copy the truncated version.
: pins the selected element so it is always displayed. When another message is received from the same Debug node, it is automatically expanded to show all pinned elements.
JSON, (JavaScript Object Notation), is a standard way for representing a JavaScript object as a String. It is commonly used by web APIs to return data.
If a message property contains a JSON string it must first be parsed to its equivalent JavaScript object before the properties it contains can be accessed. To determine whether a property contains a String or Object, the Debug node can be used.
Node-RED provides a JSON
node to do this conversion.
A common task in a flow is to modify the properties of a message as it passes
between nodes. For example, the result of an HTTP Request
may be an object with
many properties, of which only some are needed.
There are two main nodes for modifying a message, the Function node and the Change node.
The Function node allows you to run any JavaScript code against the message. This gives you complete flexibility in what you do with the message, but does require familiarity with JavaScript and is unnecessary for many simple cases. More information about writing Functions is available here.
The Change node provides a lot of functionality without needing to write JavaScript code. Not only can it modify message properties, but it can also access flow- and global-context.
It provides four basic operations:
Set
a property to a value,Change
a String property by performing a search and replace,Delete
a property,Move
a property.For the set
operation, you first identify what property you want to set, then
the value you want it to have. That value can either be a hardcoded value, such
as a string or number, or it can be taking from another message or flow/global
context property. It also supports using the JSONata expression
language to calculate a new value.
For example, using the Debug node’s ability to determine a message element’s path,
you can paste the path straight into the ‘to’ field, with msg.
selected from the
list. That will then set msg.payload
to the value of msg.payload.Phone[2].type
.
Another example, using a JSONata expression, is to convert a
temperature, held in msg.payload.temperature
, from Fahrenheit to Celsius and
store the result in a new message property msg.payload.temperature_c
.
{
"payload": {
"temperature": 90,
"temperature_c": 32.22222
}
}
Note that JSONata expressions look a lot like JavaScript, but have some key differences.
Refer to the jsonata.org site for more information.
A message sequence is an ordered series of messages that are related in some way.
For example, the Split node can turn a single message whose payload
is an Array,
into a message sequence where each message has a payload
corresponding to one
of the array elements.
msg.parts
Each message in a sequence has a property called msg.parts
. This is an object
that contains information how the message fits in the sequence. It has the following
properties:
msg.parts.id
msg.parts.index
msg.parts.count
Note: the parts array may contain additional meta-data about the sequence. For example,
the split
node also attaches information that can be used by the join
node to
reassemble the sequence. See the split
node’s documentation.
There are a number of core nodes that can work across message sequences:
Turns a single message into a sequence of messages.
The exact behaviour of the node depends on the type of msg.payload
:
Turns a sequence of messages into a single message.
The node provides three modes of operation:
Split
nodeNew in 0.18
Sorts the sequence based on a property value or JSONata expression result.
New in 0.18
Creates new sequences of messages from those received.
The node provides three modes of operation:
msg.topic
property to
identify it. The node is configured with a list of topic values to identify
the order sequences are concatenated.Node-RED: Low-code programming for event-driven applications.
Copyright OpenJS Foundation and Node-RED contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
The OpenJS Foundation | Terms of Use | Privacy Policy | OpenJS Foundation Bylaws | Trademark Policy | Trademark List | Cookie Policy