# How to add double quotes to msg.payload

**URL:** <https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022>\
**Category:** General\
**Tags:** function-node\
**Created:** [1 January 2023 13:50 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022 "2023-01-01T13:50:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [1 January 2023 13:50 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/1 "2023-01-01T13:50:43Z")

</div>

I have a function node that send msg.payload to a terminal program for communication with a 4G modem.  
How can I put " " so that the outgoing 'meg.payload +text' looks like "value my text "  
and not just like, value-my-text

The code below almost works, but I have to use - to concatenate the sentence into a 'word', but it doesn't work with negative values

```auto
  if (msg.payload < 0 && !context.sentAlert) {
       msg.payload = msg.payload.toFixed(1) +"temperature-below-freezing-point";
      context.sentAlert = true;
      return msg;
  } else if (msg.payload > 1.0) {
      context.sentAlert = false;
  }
  return null;

```

---

<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:** [1 January 2023 14:00 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/2 "2023-01-01T14:00:45Z")

</div>

JavaScript lets you use either `'` or `"` for strings. So if you want the string to contain `"`, then use `'` as the string delimiter:

```auto
msg.payload = '"' + msg.payload.toFixed(1) +'"temperature-below-freezing-point"';

```

Some other JavaScript ways of doing it:

```auto
msg.payload = `"${msg.payload.toFixed(1)} temperature-below-freezing-point"`;

```

```auto
msg.payload = "\"" + msg.payload.toFixed(1) +"\"temperature-below-freezing-point\"";

```

---

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [1 January 2023 21:24 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/3 "2023-01-01T21:24:38Z")

</div>

Thank you for your answer!

Have tested most combinations of this, without getting it to work with spaces or - characters from the temperature gauge.  
Ended up with this solution which works satisfactorily, without having understood why this works.  
Also works with negative values

```auto
msg.payload = "OutTemp," + msg.payload.toFixed(1) + "°C";

```

---

<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:** [1 January 2023 21:42 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/4 "2023-01-01T21:42:24Z")

</div>

Hi @Aurgelme

I don't see how that relates to your original question about including double quotes in a string... but I'm glad you figured out a solution to the problem.

---

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [1 January 2023 21:45 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/5 "2023-01-01T21:45:22Z")

</div>

Double quotes disappear from the output in the function node with the first solution, but are intact with this solution

Output "OutTemp,-0.3°C"

---

<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:** [1 January 2023 21:57 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/6 "2023-01-01T21:57:00Z")

</div>

@Aurgelme you are not adding `"` to your string.

If you are viewing this in the Debug sidebar, it is showing the `"` to indicate it is a string - the actual value doesn't contain them.

---

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [1 January 2023 22:23 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/7 "2023-01-01T22:23:32Z")

</div>

I'm a total noob so I don't understand the whole sequence, but I get "" these in the exec node now, and the message is sent to the phone, like this  
/4G/send-sms.js 12345678 "text message"  
"text message" is add msg.payload in exec-node

I really appreciate any corrections, so maybe there is hope for an old man to learn some programming 🙂

---

<div class="post-metadata">

**Author:** ![Aurgelme](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Aurgelme](https://discourse.nodered.org/u/Aurgelme)\
**Post date:** [9 January 2023 14:35 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/8 "2023-01-09T14:35:28Z")

</div>

@knolleary Thanks for setting me on the right track

I finally figured out the problem, firstly there was a mistake in your suggestions with one " character too much, which I should have seen myself. The second was that what I typed in the node-red editor was not correct even windows is set to English keyboard, so had to write the code line in notepad and paste the whole line into the node-red editor. (haven't found out why)  
Starting msg.payload with a text string instead of a number also solved the problem with negative values. So end up with this option.

`msg.payload = "\"Out Temp Warning " + msg.payload.toFixed(1) + "°C\"";`

---

<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:** [23 January 2023 14:35 UTC](https://discourse.nodered.org/t/how-to-add-double-quotes-to-msg-payload/73022/9 "2023-01-23T14:35:53Z")

</div>

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