Advanced join msg post split

After using "Split", how would I "Join" the message parts into 1 such that it can handle join on payload as well as some other properties of the msg.

Eg:

// with input =
{
   "_msgid": "...",
  "payload": ["a", "b"]
}

// after split, we get two msgs (this is standard functionality)
{
  "payload": "a",
  "parts": {
    "id": "a3152324.a7d68",
    "type": "array",
    "count": 2,
    "len": 1,
    "index": 0
  },
  "_msgid": "629fb3d3.85cadc"
}
    // and
{
  "payload": "b",
  "parts": {
    "id": "a3152324.a7d68",
    "type": "array",
    "count": 2,
    "len": 1,
    "index": 1
  },
  "_msgid": "629fb3d3.85cadc"
}

// then I perform some transformation on each msg and add an amount value:
{
  "payload": "a",
  "amount": 55, 
  "parts": {
    "id": "a3152324.a7d68",
    "type": "array",
    "count": 2,
    "len": 1,
    "index": 0
  },
  "_msgid": "629fb3d3.85cadc"
}
    // and
{
  "payload": "b",
  "amount": 45, 
  "parts": {
    "id": "a3152324.a7d68",
    "type": "array",
    "count": 2,
    "len": 1,
    "index": 1
  },
  "_msgid": "629fb3d3.85cadc"
}

// then I want the following output after join:
// it must join the msgs to get the original payload, as well as add the amounts
{
   "_msgid": "...",
  "payload": ["a", "b"],
  "amounts": 100  
}

Edit 1

I tried with Join node using "reduce sequence" mode, but it seems to operate on payload only

Edit 2

Added a more evolved example using "amounts" property

"indexes" will always be a list starting at 0.

Could you explain the goal of this ?

Indexes is just an example. I understand that they start from 0.

The problem is that when join node operates on msg parts, it "joins" only the payload and none of the other msg properties. So if I have some msg.key and want to "join" (along with payload) it in the output msg object I cant.

Can you give a more concrete example; ie. what is the input payload and what do you expect to come out ? You don't need the split/join node to do this.

have edited the example. Should make it easier to undersand ( i hope)

Sorry not really, where does "amounts" come from ? read too quick :wink:

Can you give the input payloads only and the expected output ? It becomes confusing because of the join node properties, the join node is not necessary if i look at the output you give in the example.

My actual use-case is much more evolved. what I am posting is a v simplified version of it.
I.e. I must split the msg and perform some transformation on payload + add some more properties like amount. then I must use join to "join" them and add the amount to produce amounts

Would it not be easier to leave it as an array and iterate over it in the function node?

Hi Colin, it would be easier to just write js, but my use cases are much more evolved. The use case posted in the question is deliberately simple

OK, to save us effort can you create a small flow of the example you showed, so inject the sample data, split it, do your transformaion, and Join it again, then show us what you get and what you want instead.

I've raise an issue here - https://github.com/node-red/node-red/issues/2735
and provided the flow.json to reproduce.
Thanks

You can set it like this

image

This doesn't work either, since the node assumes the resulting object is to be assigned to msg.payload. Try it.

Is that a problem? The messages get combined.

You could just consume the msgs from within the payload.

image

Steve - the problem is they want to use the map reduce capability to do an accumulated calculation on he way... see the issue raised for a "working" example to play with

1 Like

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