# String with Multiple Lines Convert into an Object

**URL:** <https://discourse.nodered.org/t/string-with-multiple-lines-convert-into-an-object/43005>\
**Category:** General\
**Created:** [22 March 2021 12:59 UTC](https://discourse.nodered.org/t/string-with-multiple-lines-convert-into-an-object/43005 "2021-03-22T12:59:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [22 March 2021 12:59 UTC](https://discourse.nodered.org/t/string-with-multiple-lines-convert-into-an-object/43005/1 "2021-03-22T12:59:49Z")

</div>

I'm having trouble with this string and converting it into an object so I can eventually push it into a table.

> 2021-03-22 12:39  
> W1AW  
> 14200  
> Example Comment  
> W1HW

If it was all one string on one line, I'd use the string.str() command to parse through it, but with elements on different lines, I'm not sure how to accomplish this. The 4th line down (example comment) can be a phrase or comment with variable length (usually less then 40 characters) that I'd like to keep the whole comment in tact.

Can someone point me in the right direction?

Kyle

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 March 2021 13:07 UTC](https://discourse.nodered.org/t/string-with-multiple-lines-convert-into-an-object/43005/2 "2021-03-22T13:07:24Z")

</div>

If you want an object...

```auto
msg.payload = Object.assign({}, msg.payload.split("\n"));
return msg;

```

result...

```auto
{
  '0': '2021-03-22 12:39',
  '1': 'W1AW',
  '2': '14200',
  '3': 'Example Comment',
  '4': 'W1HW'
}

```

  

If you want an array...

```auto
msg.payload = msg.payload.split("\n");
return msg;

```

result...

```auto
['2021-03-22 12:39', 'W1AW', '14200', 'Example Comment', 'W1HW']

```

  

If you want an object with named props...

```auto
var arr = msg.payload.split("\n");
msg.payload = {
 "prop1": arr[0],
 "prop2": arr[1],
 "prop3": arr[2],
 "comment": arr[3],
 "prop5": arr[4]
}
return msg;

```

result...

```auto
{
  prop1: '2021-03-22 12:39',
  prop2: 'W1AW',
  prop3: '14200',
  comment: 'Example Comment',
  prop5: 'W1HW'
}

```

* * *

a little bit javascript understanding can get you a long way.

---

<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:** [21 May 2021 13:08 UTC](https://discourse.nodered.org/t/string-with-multiple-lines-convert-into-an-object/43005/3 "2021-05-21T13:08:10Z")

</div>

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