# Merging instead of appending JSON in JOIN node after SPLIT

**URL:** https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864
**Category:** General
**Created:** [29 December 2021 18:54 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864 "2021-12-29T18:54:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![hobojoe720](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@hobojoe720](https://discourse.nodered.org/u/hobojoe720)
#### Post date: [29 December 2021 18:54 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/1 "2021-12-29T18:54:15Z")

</div>

I pull some data out of MySQL, split this and then format the results to create the JSON I need from each row and am trying to get this to merge correctly but think I am missing something here. The example flow below results in a very similar experience, where the data in is an array which is split. The split data then needs to be merged and not just added to back in if that makes sense:

The names "Main 1" and "Main 2" are not predictable, so I cant pre create anything here.

I am pretty sure I am missing something with the JOIN node that is obvious but after 2 hours I thought I would ask.

Thanks in advance

Data in

```auto
[
    {
        "Main 1": {
            "Value 1": ["a","b","c"]
        }
    },
    {
        "Main 1": {
            "Value 2": ["d","e","f"]
        }
    },
    {
        "Main 2": {
            "Value 3": ["g","h","i"]
        }
    },
    {
        "Main 2": {
            "Value 4": ["j","k","l"]
        }
    }
]

```

Desired Data Out

```auto
[
    {
        "Main 1": {
            "Value 1": ["a","b","c"],
            "Value 2": ["d","e","f"]
        }
    },
    {
        "Main 2": {
            "Value 3": ["g", "h", "i"],
            "Value 4": ["j", "k", "l"] }
    }
]

```

Very basic example flow showing the issue:

```auto
[
    {
        "id": "1c4ee0dd73fb2d38",
        "type": "inject",
        "z": "60f9277b58cf0755",
        "name": "Dummy Data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"Main 1\":{\"Value 1\":[\"a\",\"b\",\"c\"]}},{\"Main 1\":{\"Value 2\":[\"d\",\"e\",\"f\"]}},{\"Main 2\":{\"Value 1\":[\"a\",\"b\",\"c\"]}},{\"Main 2\":{\"Value 2\":[\"d\",\"e\",\"f\"]}}]",
        "payloadType": "json",
        "x": 140,
        "y": 1100,
        "wires": [
            [
                "8e6587393cbfff25"
            ]
        ]
    },
    {
        "id": "8e6587393cbfff25",
        "type": "split",
        "z": "60f9277b58cf0755",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 290,
        "y": 1100,
        "wires": [
            [
                "12ef3b2fda25129a"
            ]
        ]
    },
    {
        "id": "ce9f79b852656bad",
        "type": "debug",
        "z": "60f9277b58cf0755",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 1100,
        "wires": []
    },
    {
        "id": "12ef3b2fda25129a",
        "type": "join",
        "z": "60f9277b58cf0755",
        "name": "",
        "mode": "auto",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 530,
        "y": 1100,
        "wires": [
            [
                "ce9f79b852656bad"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 December 2021 19:13 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/2 "2021-12-29T19:13:14Z")

</div>

Try this using a change node

```auto
[{"id":"1c4ee0dd73fb2d38","type":"inject","z":"c791cbc0.84f648","name":"Dummy Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Main 1\":{\"Value 1\":[\"a\",\"b\",\"c\"]}},{\"Main 1\":{\"Value 2\":[\"d\",\"e\",\"f\"]}},{\"Main 2\":{\"Value 1\":[\"a\",\"b\",\"c\"]}},{\"Main 2\":{\"Value 2\":[\"d\",\"e\",\"f\"]}}]","payloadType":"json","x":120,"y":2440,"wires":[["c4dbab1b.b0ea48"]]},{"id":"c4dbab1b.b0ea48","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\t$keys($$.payload).{ $: $merge($$.payload[$].*)}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":2440,"wires":[["ce9f79b852656bad"]]},{"id":"ce9f79b852656bad","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":2440,"wires":[]}]

```

To do similar in join node you would need to use the reduce sequence option. At which point it is simplier to do it in a JSONata expression

---

<div class="post-metadata">

### Author: ![hobojoe720](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@hobojoe720](https://discourse.nodered.org/u/hobojoe720)
#### Post date: [29 December 2021 21:22 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/3 "2021-12-29T21:22:17Z")

</div>

That works perfect, I was not married to the idea of using the join to complete it.  
For my actual use, where the data is coming from MySQL, getting split, a function formatting it to match, then joining it again followed by the Change you posted has worked perfect.

Much appreciated

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [29 December 2021 21:26 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/4 "2021-12-29T21:26:55Z")

</div>

You can use the JOIN Node as well to reduce the array. So it is not necessary to use JSONATA to reduce an array.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [29 December 2021 21:36 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/5 "2021-12-29T21:36:56Z")

</div>

Please provide a example so I can learn. Using the OP's data

---

<div class="post-metadata">

### Author: ![hobojoe720](https://avatars.discourse-cdn.com/v4/letter/h/76d3ee/32.png) [@hobojoe720](https://discourse.nodered.org/u/hobojoe720)
#### Post date: [29 December 2021 21:59 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/6 "2021-12-29T21:59:26Z")

</div>

This would help actually if you could share an example as above. Fed in my live data and even though the formatting is identical its not playing ball. I even ensured every value throughout is unique in case that was the issue.

The results I am seeing with my live data is along the lines of this:

```auto
[
    {
        "Main 1": {
            "Value 1": ["a","b","c","d","e","f","g","h","i","j","k","l"],
            "Value 2": ["a","b","c","d","e","f","g","h","i","j","k","l"]
        }
    },
    {
        "Main 2": {
            "Value 3": ["a","b","c","d","e","f","g","h","i","j","k","l"],
            "Value 4": ["a","b","c","d","e","f","g","h","i","j","k","l"] }
    }
]

```

Edited the source data in the first post to match to make things make sense

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [29 December 2021 23:18 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/7 "2021-12-29T23:18:37Z")

</div>

@E1cid , @hobojoe720

Ok - I see - and I think your solution is at the moment the best one, as I still have problems to understand the JSONATA. Reducing the splitted array with a followed join node in reducing the sequence of messages works with JSONATA as well. I am sure that this possible - but in this case I think your (@E1cid ) change node is the most efficient solution.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [12 January 2022 23:19 UTC](https://discourse.nodered.org/t/merging-instead-of-appending-json-in-join-node-after-split/55864/8 "2022-01-12T23:19:16Z")

</div>

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