# Convert object to string

**URL:** <https://discourse.nodered.org/t/convert-object-to-string/33865>\
**Category:** General\
**Created:** [7 October 2020 04:11 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865 "2020-10-07T04:11:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephDe](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JosephDe](https://discourse.nodered.org/u/JosephDe)\
**Post date:** [7 October 2020 04:11 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/1 "2020-10-07T04:11:39Z")

</div>

Hi evereyone, I trying to figure out how to format may payload output like this { S1: "A", S2: "C" } to "A,C".  
I tried to code in a function

```auto
var myJSON = msg.payload;
var str = JSON.stringify(myJSON);
return str;

```

But not the output I was expecting.

---

<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:** [7 October 2020 05:58 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/2 "2020-10-07T05:58:40Z")

</div>

Try

```auto
var str = JSON.stringify(Object.values(myJSON));

```

---

<div class="post-metadata">

**Author:** ![JosephDe](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JosephDe](https://discourse.nodered.org/u/JosephDe)\
**Post date:** [7 October 2020 06:45 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/3 "2020-10-07T06:45:39Z")

</div>

Hi.. this is my output of my new version using the code suggested [2,3,"A,0",1] but I noticed it is not in the right order should be ["A,0",1.2,3] what do you think causes it and my end goal is to have it this format when placed in a variable _ **str1 = "A,0,1,2,3"** _ I tried to come up with a solution but no avail.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 October 2020 06:59 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/4 "2020-10-07T06:59:57Z")

</div>

> [@JosephDe](#):
>
> [2,3,"A,0",1]

What exactly was the value of `msg.payload` you had to get this result?

---

<div class="post-metadata">

**Author:** ![JosephDe](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JosephDe](https://discourse.nodered.org/u/JosephDe)\
**Post date:** [7 October 2020 07:03 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/5 "2020-10-07T07:03:38Z")

</div>

I'm getting this value in the debug {"S1":"A,0","S2":1,"S3":2,"S4":3}

---

<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:** [7 October 2020 07:15 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/6 "2020-10-07T07:15:27Z")

</div>

> [@JosephDe](#):
>
> I trying to figure out how to format may payload output like this { S1: "A", S2: "C" } to "A,C".

If S1 and S2 are always the keys to use then add a function nodes and so this...

```auto
let data = msg.payload;
let arr = [data.S1, data.S2];
msg.payload = arr.join(",");
return msg;

```

If you are saying there are a variable amount of S1, S2, S3...Sn then

```auto
let data = msg.payload;
let i = 1;
let arr = [];
while(data["S"+i] != null) {
  arr.push(data["S"+i]);
  i++;
}
msg.payload = arr.join(",");
return msg;

```

EDIT - updated (added the important `i++` - oops)

---

<div class="post-metadata">

**Author:** ![JosephDe](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JosephDe](https://discourse.nodered.org/u/JosephDe)\
**Post date:** [7 October 2020 07:23 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/7 "2020-10-07T07:23:52Z")

</div>

@Steve-Mcl thanks.. I will give it a try.

kind regards,  
Joe

---

<div class="post-metadata">

**Author:** ![JosephDe](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@JosephDe](https://discourse.nodered.org/u/JosephDe)\
**Post date:** [7 October 2020 08:30 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/8 "2020-10-07T08:30:45Z")

</div>

@Steve-Mcl..works great, thank you for the effort and saving the time! 👍

---

<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 October 2020 08:30 UTC](https://discourse.nodered.org/t/convert-object-to-string/33865/9 "2020-10-21T08:30:47Z")

</div>

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