Split

What's the Split node in Node-RED used for?

Granular data processing is important in IoT use-cases as multiple tags, for example, might be sent in one request to a server. Or, when a SQL query results in many results that need individual processing. In Node-RED, flows are message based, but a message can be split in multiple messages if needed using the Split node. The split node is one of the core nodes in Node-RED, thus installed by default. It's a fundamental building block for powerful automations.

The Split node is used to divide a single message into multiple messages based on defined rules.

The Split node in Node-RED is used to split an incoming message object into several different message objects. The incoming object can be a simple string, an array, or an object. The Split node will split the object based on the following criteria:

  • String: The Split node will split the string on a delimiter character. The delimiter character can be specified in the node's configuration.
  • Array: The Split node will split the array into a series of messages, each containing one element of the array.
  • Object: The Split node will split the object on the keys of the object. The keys of the object can be specified in the node's configuration.

The Split node can be used to process data in a variety of ways. For example, it can be used to split a string of text into a series of messages, each containing one word of the text. It can also be used to split an array of data into a series of messages, each containing one element of the array.

The following are some examples of how the Split node can be used:

  1. Splitting a string of text into a series of messages, each containing one word of the text.
  2. Splitting an array of data into a series of messages, each containing one element of the array.
  3. Splitting an object on the keys of the object, to create a series of messages, each containing one key-value pair of the object.
  4. Splitting a message on a delimiter character, to create a series of messages, each containing one part of the message.

The Split node is a powerful tool that can be used to process data in a variety of ways. It is a valuable addition to any Node-RED flow.

Examples

Splitting arrays

The easiest way split a message into multiple is when the structure of the input message is naturally split into multiple. Arrays are an example of such structures. Creating an output message for each element is the default mode for Split. When inputting one array with 1, 2, 3, 4 as contents, 4 messages will be printed in order:

Regrouping elements

When a large number of elements need regrouping, the split node can be used to chunk the data. For example, some API's will only let you update 20 records at the time. If you have more input, splitting its sub-groups will divide it into multiple messages. This is done by setting Fixed length of to the maximum size of the sub-group. Say the payload to split is [1, 2, 3, 4, 5] with a Fixed length of set to 2. This will send 3 messages: [1, 2], [3, 4], and [5].

Splitting Strings

Strings are able to be split in multiple ways, most commonly by line. Though it's also possible to split them by a fixed length, which is useful when data is structured predicatably.

Let's start with splitting by line, as this is the default way the node operates. Keep the Split using set to \n, which is the line ending character in Unix based systems. In our next example, we have a list of 3 cities in Europe, which we need individually.

Note we use the template node here simply, as the "Inject" node does not allow you to create multi-line strings.

Splitting by word

When you've split something by line, you might need to split by word. Simply putting a space in the Split using setting. That doesn't show a visible character in the form, as opposed to the line ending character.

Splitting Objects

The remaining data structure of the Node-RED split node is "Objects", which is the catch-all category. Node-RED uses hashes, key-value pairs of property names and values. JSON is example of this. In the example below, we'll split the mapping of the words "one" and "two" versus the number values.

Node Documentation

Splits a message into a sequence of messages.

Inputs

payloadobject | string | array | buffer
The behaviour of the node is determined by the type of msg.payload:
  • string/buffer - the message is split using the specified character (default: \n), buffer sequence or into fixed lengths.
  • array - the message is split into either individual array elements, or arrays of a fixed-length.
  • object - a message is sent for each key/value pair of the object.

Outputs

partsobject
This property contains information about how the message was split from the original message. If passed to the join node, the sequence can be reassembled into a single message. The property has the following properties:
  • id - an identifier for the group of messages
  • index - the position within the group
  • count - if known, the total number of messages in the group. See 'streaming mode' below.
  • type - the type of message - string/array/object/buffer
  • ch - for a string or buffer, the data used to the split the message as either the string or an array of bytes
  • key - for an object, the key of the property this message was created from. The node can be configured to also copy this value to another message properties, such as msg.topic.
  • len - the length of each message when split using a fixed length value

Details

This node makes it easy to create a flow that performs common actions across a sequence of messages before, using the join node, recombining the sequence into a single message.

It uses the msg.parts property to track the individual parts of a sequence.

Streaming mode

The node can also be used to reflow a stream of messages. For example, a serial device that sends newline-terminated commands may deliver a single message with a partial command at its end. In 'streaming mode', this node will split a message and send each complete segment. If there is a partial segment at the end, the node will hold on to it and prepend it to the next message that is received.

When operating in this mode, the node will not set the msg.parts.count property as it does not know how many messages to expect in the stream. This means it cannot be used with the join node in its automatic mode.