Split string into object array or variables

Hi

I get the type of string below returned to me from a mqtt server, always 5 elements, it cannot be modified.
receipt=xxxxxxr5wfe51zkxxrow97nsox4xxxx&acknowledged=1&acknowledged_at=1727167256&acknowledged_by=xxxxxxnumm3acmuxxxxxxxxxxxx&acknowledged_by_device=steve mobile

I have tried splitting with the split node using the "&" and that gives me 5 seperate messages all called msg.payload. What I am trying to get is either 5 variables or an object array, so that I can further process each pair. Not looking for someone to the write the code but just some guidance on the way to go. Forum and google search have just added to my confusion as still early days with Nodered.
The goal is to be able to log when & if a Pushover message has been acknowledged and by whom. As far as i am aware ampersands are not returned within the string unless they are used as delimiters (joiners?)
Thanks

You need to use the split and join nodes.

Split makes one message per section of your input, join can be configured to output a key: value formatted object or an array.

For this I'd probably just pass it through a change node and use JSONata...

$reduce(
  $split(payload, "&"),
  function($accumulator, $pair) {
    $merge([$accumulator, {$split($pair, "=")[0]: $split($pair, "=")[1]}])
  },
  {}
)
[{"id":"e5ead10e6f70c434","type":"inject","z":"43ec589e241ae3ab","name":"receipt=xxxxxxr5wfe.....","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"receipt=xxxxxxr5wfe51zkxxrow97nsox4xxxx&acknowledged=1&acknowledged_at=1727167256&acknowledged_by=xxxxxxnumm3acmuxxxxxxxxxxxx&acknowledged_by_device=steve mobile","payloadType":"str","x":1740,"y":80,"wires":[["d6917469d8c93e55"]]},{"id":"d6917469d8c93e55","type":"change","z":"43ec589e241ae3ab","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$reduce(   $split(payload, \"&\"),   function($accumulator, $pair) {     $merge([$accumulator, {$split($pair, \"=\")[0]: $split($pair, \"=\")[1]}])   },   {} )","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1760,"y":140,"wires":[["8adc6ffbf919c182"]]},{"id":"8adc6ffbf919c182","type":"debug","z":"43ec589e241ae3ab","name":"debug 199","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1770,"y":200,"wires":[]}]


Alternatively, in a function

msg.payload = msg.payload.split('&').reduce((acc, pair) => {
    const [key, value] = pair.split('=');
    acc[key] = value;
    return acc;
}, {});
return msg;
1 Like

Or without the merge and reduce function

$split($$.payload, "&").${$split($, "=")[0]: $split($, "=")[1]}

[edit]
Or with a few more $, just to confuse.

$split($$.payload, "&").[$split($,"=")].${$[0]: $[1]}
1 Like

Hi

Thanks for your quick response,
Tried various options with the join node but only managed to get the string broken down into a string array rather than an object array, I probably need some extra settings within the join node.

way better (I tried JSONata for once)

Hello

Appreciate the speedy reply, the function node works perfectly, thank you. The MQTT message also includes a topic and this appears to change the way the JSONATA code reacts in that the object includes the topic (hope that makes sense) and than an array with index nos. Will have a play with all the options and try to learn a little more.

Thanks for your assistance.

Thanks for the solution.

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