In Node-RED, there are always many ways to restructure your data. My favorite is to use a JSONata expression inside a change
node, like so:
Using the Expression Test tab with your data, you can see the results in real-time as you type the expression in the window above -- very nice!
Although the expressions are very powerful, the lambda syntax of the language itself can be a bit puzzling to read... Here is a short description of what this expression does:
payload.$split('<br>').[$split('=')] {
$[0]: $[0] in ['SER', 'FW'] ? $[1]
: $[0] = 'TS' ? $fromMillis($number($[1])*1000)
: $number($[1])
}
-
Line 1: split the payload into an array of "key=value" strings, and then split each of those strings into a 2-element array of
["key", "value"]
pairs
-
Lines 1 - 5: from the opening
{
to closing }
is the body of a single output object, with:
-
Line 2: field name pulled from the first pair element
$[0]
, with 'SER' and 'FW' field values being the string in the second pair element $[1]
-
Line 3: if the field name is 'TS' then multiply the value of secs. by 1000 to get millis, and convert to a datetime value
-
Line 4: for all other field names, just get the numeric value of the string
Most people would stick with plain Javascript for maintainability, but I just wanted to show an alternative, which I think is worth learning. Hope this helps more than it confuses!