# Format output of array with carriage returns

**URL:** <https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726>\
**Category:** General\
**Created:** [3 February 2022 15:11 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726 "2022-02-03T15:11:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slurpey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/slurpey/32/16320_2.png) [@slurpey](https://discourse.nodered.org/u/slurpey)\
**Post date:** [3 February 2022 15:11 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726/1 "2022-02-03T15:11:44Z")

</div>

Hi, I've been banging my head on this without much success.

The output of my payload will be sent to whatsapp. But it lists everything with a comma and I'd like a carriage return.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93c6a309409641d558ba2217f4f6fdd0fcc20597.png)

```auto
[{"id":"c2ca69f3dbe80770","type":"api-call-service","z":"5c26fb4772d3c08c","name":"Whatsapp","server":"a0eea9da.0fe1d8","version":3,"debugenabled":false,"service_domain":"notify","service":"whatsapp","entityId":"","data":"{\"message\":\"{{payload.attributes.entities}}\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":740,"y":420,"wires":[[]]},{"id":"1f76c24e88ac0bea","type":"inject","z":"5c26fb4772d3c08c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":440,"wires":[["fc6e9e334e49f3d0"]]},{"id":"fc6e9e334e49f3d0","type":"api-current-state","z":"5c26fb4772d3c08c","name":"Unavailablke sensor","server":"a0eea9da.0fe1d8","version":3,"outputs":2,"halt_if":"2","halt_if_type":"str","halt_if_compare":"does_not_include","entity_id":"sensor.unavailable_entities","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","x":500,"y":440,"wires":[["fc20e36941d596c7","c2ca69f3dbe80770"],[]]},{"id":"fc20e36941d596c7","type":"debug","z":"5c26fb4772d3c08c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":480,"wires":[]},{"id":"a0eea9da.0fe1d8","type":"server","name":"GOOD Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

```

The output in whatsapp is hard to read, because it's seperated by a comma instead of new line

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06db40aa69c0a1e242ea664d46635eced054f5ef.png)

I'm lacking competence in the functions to change this - I tried several things but they don't work...  
My debug info

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/7/6746fb665f1bfadedb161d85879f5fa5df64320a.png)

Any suggesiton please?

---

<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:** [3 February 2022 15:39 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726/2 "2022-02-03T15:39:09Z")

</div>

I think the HA node allows JSONata  
so you could try

```auto
{"message": $join($$.payload.attributes.entities, "\n")}

```

or in a change node before  
Set `msg.payload.attributes.entities`  
To JSONata expression `$join($$.payload.attributes.entities, "\n")`

---

<div class="post-metadata">

**Author:** ![slurpey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/slurpey/32/16320_2.png) [@slurpey](https://discourse.nodered.org/u/slurpey)\
**Post date:** [3 February 2022 16:41 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726/3 "2022-02-03T16:41:33Z")

</div>

Hi, thanks, just tested.  
this first one results in empty topic  
`{"message": $join($$.payload.attributes.entities, "\n")}`  
the tester in nodered returns

> Invalid example JSON message:  
> SyntaxError: Unexpected token $ in JSON at position 12

But... the second one worked 🙂 with a change node.

```auto
[{"id":"c2ca69f3dbe80770","type":"api-call-service","z":"5c26fb4772d3c08c","name":"Whatsapp","server":"a0eea9da.0fe1d8","version":3,"debugenabled":false,"service_domain":"notify","service":"whatsapp","entityId":"","data":"{\"message\":\"{{payload.attributes.entities}}\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":760,"y":380,"wires":[[]]},{"id":"1f76c24e88ac0bea","type":"inject","z":"5c26fb4772d3c08c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":440,"wires":[["fc6e9e334e49f3d0"]]},{"id":"fc6e9e334e49f3d0","type":"api-current-state","z":"5c26fb4772d3c08c","name":"Unavailablke sensor","server":"a0eea9da.0fe1d8","version":3,"outputs":2,"halt_if":"2","halt_if_type":"str","halt_if_compare":"does_not_include","entity_id":"sensor.unavailable_entities","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","x":500,"y":440,"wires":[["80a495f7dd6a26c8"],[]]},{"id":"fc20e36941d596c7","type":"debug","z":"5c26fb4772d3c08c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":420,"wires":[]},{"id":"80a495f7dd6a26c8","type":"change","z":"5c26fb4772d3c08c","name":"Clean","rules":[{"t":"set","p":"msg.payload.attributes.entities","pt":"msg","to":"$join($$.payload.attributes.entities, \" \")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":380,"wires":[["c2ca69f3dbe80770","fc20e36941d596c7"]]},{"id":"a0eea9da.0fe1d8","type":"server","name":"GOOD Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

```

Except... that whatsapp api built for Home Assistant - or whatsapp API itself doesn't allow the special character... Lots of discussions online about it - tried different things   
 and %0a notably but none worked ☹  
So I resolved myseld to add 5 spaces in your change node instead of the line break and it's at least much better.

Thanks a lot.

---

<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:** [3 February 2022 17:46 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726/4 "2022-02-03T17:46:42Z")

</div>

> [@slurpey](#):
>
> Invalid example JSON message:  
> SyntaxError: Unexpected token $ in JSON at position 12

you need to set to JSONata not JSON

not sure what whatsapp uses as i don't use it, but you could try html `<br>`or markup `\`

---

<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:** [4 April 2022 17:47 UTC](https://discourse.nodered.org/t/format-output-of-array-with-carriage-returns/57726/5 "2022-04-04T17:47:43Z")

</div>

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