# Spit a payload and get rid of the last bracket

**URL:** <https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721>\
**Category:** General\
**Created:** [14 August 2021 19:14 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721 "2021-08-14T19:14:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [14 August 2021 19:14 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/1 "2021-08-14T19:14:26Z")

</div>

Hi I am having a string

```auto
0.1.1.1|00:03:CD:01:6F:BA (Mobotix AG)
0.1.1.111|C2:4D:69:BB:65:A4 (American Power Conversion)
0.1.1.133|7C:87:AB:CD:EF:GH (Hewlett Packard)
............

```

Then I am trying to brin the strin into IP, MAC and hardware vendor.

```auto
let response = msg.payload.split('\n');
let found = []
let device
response.forEach ( line => {
    if ( line.indexOf('|') > -1 ){
        device = {
            IP : line.split('|')[0],
            MAC: line.split('|')[1].split(' ')[0],
            HW: line.split('(')[1],
        }
        found.push ( device )
    }
})
msg.payload = found;
return msg;

```

the resault:

the IP, The MAC and Mobotix AG)  
the IP, The MAC and American Power Conversion)  
the IP, The MAC and Hewlett Packard)

BUT how can i get rid of the last closing bracket?

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [14 August 2021 19:29 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/2 "2021-08-14T19:29:13Z")

</div>

Hi, i would use line.substring() and line.findFrist and line.IndexOf() to search for | and the space

> **[String.prototype.substring() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring)**
>
> The substring() method returns the part of the
> string between the start and end indexes, or to the end of the string.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [14 August 2021 19:32 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/3 "2021-08-14T19:32:31Z")

</div>

With slice you can remove the right characters

```auto
var mystring = "Hello world!";
msg.payload = mystring.slice(0,-1);

```

Will result in "Hello world" by removing 1 character from the right

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [14 August 2021 20:49 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/4 "2021-08-14T20:49:04Z")

</div>

THX  
@ [Christian-Me](https://discourse.nodered.org/u/Christian-Me)  
i have not been able to get it to work properly in my existing function.  
I have taken edje11 solution, because here it is always just a sign sign and it does not change.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 August 2021 21:33 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/5 "2021-08-14T21:33:53Z")

</div>

Here is an alternative matching based on regular expression grouping

```javascript
msg.payload = msg.payload.split('\n')

let results = []

msg.payload.forEach(line => {
    let group = line.match(/(.+)\|(.+)\s\((.*)\)/)
   if( group && group.length > 3) results.push({ id: group[1], ip: group[2], name: group[3] })
});

msg.payload = results;
return msg;

```

---

<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:** [14 August 2021 21:52 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/6 "2021-08-14T21:52:42Z")

</div>

Or split the lines using regex to

```auto
let lines = msg.payload.split('\n'); // you could also do ')\n'
msg.payload=[];
lines.forEach ( line => {
    let parts = line.split(/\||\(|\)/); // and here /\||\(/
    if ( parts.length >= 3 ){ 
        msg.payload.push({
            IP : parts[0].trim(),
            MAC: parts[1].trim(),
            HW: parts[2].trim(),
        })
    }
})
return msg;

```

@UnborN the last match on line 4 of the string, will result in `group` being null, you need to add a conditional to lookout for a null match.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 August 2021 22:11 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/7 "2021-08-14T22:11:53Z")

</div>

> [@E1cid](#):
>
> the last match on line 4 of the string, will result in `group` being null

i didn't include the 4th line in the test as i considered that it was just there to imply that there were more lines  
but yes, you are right, its good to check for `null` or length \>= 3  
(Edited the above post)

---

<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:** [28 August 2021 22:12 UTC](https://discourse.nodered.org/t/spit-a-payload-and-get-rid-of-the-last-bracket/49721/8 "2021-08-28T22:12:25Z")

</div>

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