# Help with splitting a payload

**URL:** https://discourse.nodered.org/t/help-with-splitting-a-payload/6043
**Category:** General
**Created:** [21 December 2018 16:22 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043 "2018-12-21T16:22:37Z")
**Posts on this page:** 12
**Page:** 2

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [23 December 2018 00:04 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/21 "2018-12-23T00:04:22Z")

</div>

> [@Lost](#):
>
> I have no control over how the payload is generated

Have you considered what I suggested earlier about using Regex to sort that?

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 00:07 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/22 "2018-12-23T00:07:54Z")

</div>

No worries, I just finished a workaround and coded the exact suggestion you did (adding a hash for channel in between msg.sender.id and msg.sender.type).

Note that four new properties are added to the message:

![a-02](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex026/uploads/nodered/original/2X/a/a43bfb5e563c92af8658383cccf3a1a55a54c83b.png)

Please check if you manage to import and test this flow: [exported-flow-2.txt](https://discourse.nodered.org/uploads/short-url/1Z0CDLrCddSwE3Lmu8KGmMpUl6x.txt) (163.6 KB)

---

<div class="post-metadata">

### Author: ![Lost](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lost](https://discourse.nodered.org/u/Lost)
#### Post date: [23 December 2018 00:10 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/23 "2018-12-23T00:10:07Z")

</div>

It was just a simple way i used to filter out the messages i dont need. Some of the messages have empty payload. So in the begining when i tryed i just checked for the word success first. but not sure it is needed. Have considered and tryed to fix it but had no idea how to do it. Cant really understand how your fix works but will play with it tomorrow and see if i can learn from it and use it. Thanks for the example.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 00:10 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/24 "2018-12-23T00:10:53Z")

</div>

> [@Lost](#):
>
> I have no control over how the payload is generated

Hi Paul,

Thanks, but as @Lost said it is not doable to change the payload upfront I generated the required data with other ~~JavaString~~ JavaScript methods.

---

<div class="post-metadata">

### Author: ![Lost](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lost](https://discourse.nodered.org/u/Lost)
#### Post date: [23 December 2018 01:09 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/25 "2018-12-23T01:09:51Z")

</div>

That works great. You make it look to easy when i have been strugeling to get it to work without success, but have learned a bunch.

Thanks again.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 01:14 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/26 "2018-12-23T01:14:26Z")

</div>

Great. As a matter of fact, I have learned too from this interesting use case, so thank you.

As it is working well you may want to change this setting in the switch node. It will make the node to stop checking the rules when the match is done. It will save a few CPU cycles.

 ![a-03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2672c9373e031b7027e0f5d045b8fd2df865e842.png)

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [23 December 2018 09:51 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/27 "2018-12-23T09:51:57Z")

</div>

> [@Andrei](#):
>
> Hi Paul,
> 
> Thanks, but as @Lost said it is not doable to change the payload upfront I generated the required data with other ~~JavaString~~ JavaScript methods.

Hi Andrei.

I've not suggested that the payload is changed upfront!  
I'm using regex to put the existing payload into a valid json format, and that then enables it to be parsed correctly.

So using regex in my example above;

```auto
payload: "{ channel: 1, success: true, level: 66 }"

```

becomes;

```auto
object
channel: 1
success: true
level: 66

```

which is then much easier and more reliable to parse.

_I'm not suggesting that my idea is better than yours 😉 but I spent over an hour working on this (my own learning experience), and wondered why you were dismissive of this approach._

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 12:40 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/28 "2018-12-23T12:40:36Z")

</div>

OMG, my bad Paul. I thought you were only giving an example of how the ideal payload would look like. I did not know (I did not connect the dots) that the REGEX you proposed would already do the transformation. The solution I used to break down the payload is not ideal as it is susceptible to the payload construction. If tomorrow the owner of `node-red-contrib-hdl` removes a single space from the payload my the solution will stop working. Your solution is a way more robust. The problem to transform this string `"{ channel: 1, success: true, level: 66 }"` into a JSON or a JavaScript object is not trivial indeed. I spend some time trying to transform it on a perfect formatted JSON but was defeated and I gave up. Then I considered to transform it in an object using eval() or Function() but every reference in the internet discouraged using such method. Finally, I ended up using split() even not being a fan of this kind of usage. Thank you so much. 😉

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 13:35 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/29 "2018-12-23T13:35:06Z")

</div>

Hi Paul, I am playing with your REGEX as certainly, I will use it in the future.

I guess I will need to refresh my knowledge on REGEX as I could not get how come that a single and short line like this `var a = pay.replace((/([\w]+)(:)/g), "\"$1\"$2");` can be so "smart" to add double quotes only in the property keys (not in the values). It is brilliant.

![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3cb914ee11f82779d459a477c8d591a4078f9508.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [23 December 2018 13:52 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/30 "2018-12-23T13:52:38Z")

</div>

It looks for the ':' and then puts the double quotes around the word before it.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [23 December 2018 15:49 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/31 "2018-12-23T15:49:30Z")

</div>

I see now. Very smart indeed. Thanks !

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [23 December 2018 17:24 UTC](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043/32 "2018-12-23T17:24:05Z")

</div>

The second Regex rule - `(a.replace((/'/g), "\""))` isn't really necessary in this case, but as we have no control over the format of the `node-red-contrib-hdl` payload, I added it to ensure that any single quotes `'` are replaced by double quotes `"` should the format change in future.

Once in the correct json format, I'm using JSON.parse to create a json object.

Paul

[Previous page](https://discourse.nodered.org/t/help-with-splitting-a-payload/6043.md?page=1)
