# Object to string issue

**URL:** <https://discourse.nodered.org/t/object-to-string-issue/59064>\
**Category:** General\
**Created:** [27 February 2022 14:07 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064 "2022-02-27T14:07:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [27 February 2022 14:07 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/1 "2022-02-27T14:07:29Z")

</div>

I need to change an object to string but I am not getting out the desired output.  
My object is like this:  
 ![Schermata 2022-02-27 alle 15.44.19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf38cab4c363d7a2072a5ac9e5d4f27393d96194.png)

I am using a function to stringify the object and strip out/replace the unwonted chars. For some reason I am not getting out the result I expect.

This is the function I am using:

```auto
var st=msg.payload;
output=JSON.stringify(st);
output=output.replace('{','');
output=output.replace('}','');
output=output.replace(',',':');
output=output.replace('"','');
msg.payload=output;
return msg;

```

I have tried replacing and removing the chars using the String Node and also using the above function (at present written step by step).

This is what I am getting out of the above function:  
**gpio17":1:"gpio13":0,"gpio27":1,"gpio22":0**  
This is what I would like to get out:  
**gpio13:0:gpio27:1:gpio17:1:gpio22:0**

I am not sure why it is not working. Any help will be appreciated. Thanks.

```auto
[{"id":"cec70e3f10d1ba16","type":"function","z":"530a8a0554240fad","name":"","func":"var st=msg.payload;\noutput=JSON.stringify(st);\noutput=output.replace('{','');\noutput=output.replace('}','');\noutput=output.replace(',',':');\noutput=output.replace('\"','');\nmsg.payload=output;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":560,"wires":[["0a872df79f52613b"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 February 2022 14:32 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/2 "2022-02-27T14:32:17Z")

</div>

This uses a Json node and a change node. It doesn't change the order of the input data to 13 , 27, 17, 22 though, not sure how to do that:

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea622d49f9e62f8585c07b2f0497ca9a9b4d4e92.jpeg)

```auto
[{"id":"a43c6c2847cbebb0","type":"inject","z":"4be637d7361f36e2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"gpio13\":0,\"gpio17\":1,\"gpio22\":0,\"gpio27\":1}","payloadType":"json","x":390,"y":120,"wires":[["0b14087deac59b0c","8ee058c9441eb18e"]]},{"id":"0b14087deac59b0c","type":"json","z":"4be637d7361f36e2","name":"","property":"payload","action":"","pretty":false,"x":530,"y":120,"wires":[["d0f55c14ebcc5ee2"]]},{"id":"b60ef364c5c0b191","type":"debug","z":"4be637d7361f36e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":120,"wires":[]},{"id":"d0f55c14ebcc5ee2","type":"change","z":"4be637d7361f36e2","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":",","fromt":"str","to":":","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"[{}\"]","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":120,"wires":[["b60ef364c5c0b191"]]},{"id":"8ee058c9441eb18e","type":"debug","z":"4be637d7361f36e2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":80,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [27 February 2022 14:48 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/3 "2022-02-27T14:48:38Z")

</div>

Alternatively if you want to continue using the `function` node:

```auto
var st=msg.payload;
let output = JSON.stringify(st);
output=output.replace('{','');
output=output.replace('}','');
output=output.replaceAll(',',':');
output=output.replaceAll('"','');
msg.payload=output;
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:** [27 February 2022 14:53 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/4 "2022-02-27T14:53:15Z")

</div>

You need to set the g modifier to global replace, otherwise it just the first match

```auto
var st=msg.payload;
output=JSON.stringify(st);
output=output.replace(/\{|}|"/g,'');
output=output.replace(/,/g,':');
msg.payload=output;
return msg;

```

Or  
you could do it in a change node with JSONata expression

```auto
$join($spread($zip($keys(payload), payload.*.$string($))),":")

```

Or If you want the same using Javascript, but don't wish to use regex

```auto
msg.payload = Object.entries(msg.payload).reduce((a,e,i,o) => {
   return a += e[0] + ":" + e[1] + (i < o.length-1 ? ":" : "")
},"")
return msg;

```

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [27 February 2022 14:59 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/5 "2022-02-27T14:59:24Z")

</div>

Regex is still quite new to me, but could you combine lines 3 and 4 in any way that would still work?

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [27 February 2022 15:02 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/6 "2022-02-27T15:02:15Z")

</div>

> [@E1cid](#):
>
> ```auto
> var st=msg.payload;
> output=JSON.stringify(st);
> output=output.replace(/\{|}|"/g,'');
> output=output.replace(/,/g,':');
> msg.payload=output;
> return msg;
> 
> ```

Thank you a million. This did work perfectly.

---

<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:** [27 February 2022 15:03 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/7 "2022-02-27T15:03:18Z")

</div>

Yes it is possible with the replace as a call to a function , but seems a bit overkill. for one extra replace.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [27 February 2022 15:14 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/8 "2022-02-27T15:14:06Z")

</div>

Could you explain why the replacements of the `,`'s for `:`'s has to be separate from the others? Is it because you want a different end result?

EDIT:  
Never mind, It appears that the use of regex is only allowed for the first substring and not the replacement substring (meaning that you cannot set it to more than one end result).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 February 2022 15:20 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/9 "2022-02-27T15:20:19Z")

</div>

> [@FeliceM](#):
>
> This is what I would like to get out:  
> **gpio13:0:gpio27:1:gpio17:1:gpio22:0**

Can you explain why you want that, it seems an unusual requirement?

---

<div class="post-metadata">

**Author:** ![FeliceM](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@FeliceM](https://discourse.nodered.org/u/FeliceM)\
**Post date:** [27 February 2022 16:06 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/10 "2022-02-27T16:06:32Z")

</div>

I am sending data via MQTT. Since all the other data I am sending are strings, I thought to unify everything as string. Probably I don't need the name of the GPIO to be sent but I need to make sure they do not change position/order first.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 February 2022 16:48 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/11 "2022-02-27T16:48:43Z")

</div>

> [@FeliceM](#):
>
> I am sending data via MQTT.

If you pass the original object to the MQTT node it will convert it to a JSON string for you and send that. At the other end (if it is node-red) then again the MQTT node can convert it back to an object. If it isn't node-red then most toolchains can understand JSON strings in some 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:** [13 March 2022 16:48 UTC](https://discourse.nodered.org/t/object-to-string-issue/59064/12 "2022-03-13T16:48:48Z")

</div>

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