Working with Strings in Node-RED
Strings are one of the most common data types in Node-RED. Whether you're converting sensor values, parsing API responses, or building dynamic messages, understanding string operations is essential for building reliable flows.
Converting String to Number
One of the most frequent operations is converting string values to numbers for mathematical calculations or comparisons.
- Connect your data source to a Change node
- In the Change node, set the rule to "Set"
msg.payload - Select "to the value of" and choose JSONata expression
- Enter:
$number(payload) - Connect to where you need the processed data
If your payload contains the string "42", it becomes the number 42. You can now use this in calculations or comparisons.
Converting Number to String
Converting numbers to strings is useful for displaying values, building messages, or formatting output.
- Connect your data source to a Change node
- Set the rule to "Set"
msg.payload - Select JSONata expression
- Enter:
$string(payload) - Connect to where you need the processed data
A number like 42 becomes the string "42", ready for text operations.
Splitting Strings
Splitting strings is essential when parsing CSV data, breaking apart delimited values, or extracting specific parts of text.
- Connect your string data source to a Split node
- Double-click the Split node to open its configuration
- In the "Split using" field, enter your delimiter character:
,for comma-separated values (CSV)(space) for splitting words\nfor splitting by lines\tfor tab-separated values (TSV);for semicolon-separated data|for pipe-delimited data
- Click Done
- Connect to where you need the processed data
The Split node creates separate messages for each segment. For example, if you receive a log entry "2024-12-15 14:30:45 ERROR Database connection failed" and you split using spaces, it produces separate messages: first "2024-12-15", then "14:30:45", then "ERROR", then "Database", and so on. Each piece flows through your subsequent nodes one at a time, allowing you to extract the timestamp, severity level, and message separately.
Concatenating Strings
Combining strings is common when building messages, URLs, or formatted output.
- Add a Template node after the nodes containing your data
- Double-click to open the Template configuration
- Write your text and insert variables using
{{variableName}}syntax - Click Done
- Connect to where you need the processed data
Each {{variableName}} is replaced with actual data. For example, the template Hello {{payload.name}}, your order #{{payload.orderId}} has shipped to {{payload.city}}. with data containing name "Sarah", orderId "12345", and city "Portland" produces: Hello Sarah, your order #12345 has shipped to Portland.
Parsing JSON Strings
API responses and stored data often arrive as JSON strings—text that looks like JSON but isn't yet usable as an object.
- Place a JSON node after your data source (like an HTTP request or file read)
- Double-click to open its configuration
- Set the Action to "Convert between JSON String & Object"
- Click Done
- Connect to where you need the processed data
The JSON node detects your data type automatically. String '{"temperature":22,"humidity":65}' becomes an object {temperature: 22, humidity: 65} so you can access msg.payload.temperature.
Extracting Substrings
Getting specific parts of a string is useful for parsing fixed-format data, extracting codes, or isolating values.
- Add a Change node after your string source
- Set the rule to "Set"
msg.payload - Select JSONata expression
- Enter:
$substring(payload, start, length)where:startis the position (0 is first character)lengthis how many characters to take
- Connect to where you need the processed data
Examples:
$substring(payload, 0, 5)on"Hello World"gives"Hello"$substring(payload, 6, 5)on"Hello World"gives"World"$substring(payload, 6)(no length) on"Hello World"gives"World"(all remaining characters)
Trimming Whitespace
Removing unwanted spaces, tabs, or line breaks from strings prevents comparison errors and formatting issues.
- Place a Change node before your comparison or processing logic
- Set the rule to "Set"
msg.payload - Select JSONata expression
- Enter:
$trim(payload) - Connect to where you need the processed data
Whitespace from both ends is removed. " Hello World " becomes "Hello World". The space between words stays—only edge spaces are removed.
Changing Case
Converting string case helps with standardization and comparison since computers treat uppercase and lowercase as different.
- Add a Change node before your comparison or output
- Set the rule to "Set"
msg.payload - Select JSONata expression
- If you want to convert to uppercase, enter:
$uppercase(payload). If you want to convert to lowercase, enter:$lowercase(payload) - Connect to where you need the processed data
Using $uppercase(payload), the string "hello world" becomes "HELLO WORLD". Using $lowercase(payload), the string "Hello World" becomes "hello world".
Replacing Text
Finding and replacing text within strings lets you correct values, standardize formats, or update content.
- Add a Change node after your text source
- Set the rule to "Set"
msg.payload - Select JSONata expression
- Enter:
$replace(payload, "old", "new")where "old" is text to find and "new" is the replacement - Connect to where you need the processed data
All occurrences are replaced. "I love apples and apples are great" with $replace(payload, "apples", "oranges") gives "I love oranges and oranges are great".
Checking String Length
Determining string length helps with validation or conditional processing.
- Add a Change node that will store the length
- Set the rule to "Set"
msg.length(or another property) - Select JSONata expression
- Enter:
$length(payload) - Connect to where you need the processed data
The string "Hello" returns 5. Use this value in conditions or validation logic.
Checking if String Contains Text
Testing whether a string contains specific text helps with filtering and conditional logic.
- Add a Change node to create a test result
- Set the rule to "Set"
msg.contains(or another property) - Select JSONata expression
- Enter:
$contains(payload, "search term") - Connect to where you need the processed data
Returns true if found, false if not. "The quick brown fox" with $contains(payload, "quick") returns true. Use in Switch nodes to route messages differently based on content.
Complex String Operations
For complex string operations that combine multiple steps or require custom logic, you can use a Function node with JavaScript.
If you're not familiar with JavaScript, but you're using FlowFuse, you can use the FlowFuse Assistant's function node generator. Simply describe what you want to accomplish, and the assistant will generate the function node code for you.