# Is there a neater way of creating this payload?

**URL:** https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809
**Category:** General
**Created:** [18 December 2023 14:19 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809 "2023-12-18T14:19:15Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)
#### Post date: [18 December 2023 14:19 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/1 "2023-12-18T14:19:15Z")

</div>

Hi

I am creating table in a database, originally it was one long line ,  
I wanted to make it clearer to understand as I do not like one long line of text.  
I have made a function node with this, it works but is there a better and neater way  
As I have lots of commas and pluses ?

```auto
msg.payload = 
	
	"CREATE TABLE sensors (" +
	"serial_no SERIAL PRIMARY KEY," +
	"device TEXT not null," +
	"device_data JSON not null );"

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: [18 December 2023 14:30 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/2 "2023-12-18T14:30:36Z")

</div>

You could use a [template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals), as sql statement can be on multiple lines

```auto
msg.payload = `CREATE TABLE sensors (
  serial_no SERIAL PRIMARY KEY,
  device TEXT not null,
  device_data JSON not null 
);`

```

The quotes are backticks

---

<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: [18 December 2023 14:50 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/3 "2023-12-18T14:50:18Z")

</div>

I believe that will generate a mulit-line string with Return characters in it.

@questuk you can do this if you prefer it

```auto
msg.payload = "CREATE TABLE sensors ( \
  serial_no SERIAL PRIMARY KEY, \
  device TEXT not null, \
  device_data JSON not null );"

```

Edit: I see that @E1cid acknowledged that and says that an SQL statement can include embedded return characters. He may well be correct.

---

<div class="post-metadata">

### Author: ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)
#### Post date: [18 December 2023 15:23 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/4 "2023-12-18T15:23:19Z")

</div>

Hi, E1cid

Yes that works ... thank you and colin, I thought it should be something like that.  
I did have that exact layout of text, but it would fail.

I was using ' ' and not `` very subtle but makes or breaks code 🙂

Thank you both very much for your help, that code will be added to my " cheat sheet " for future reference.  
👍

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [19 December 2023 03:23 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/5 "2023-12-19T03:23:16Z")

</div>

I like to use the `template` node for blocks of text like this -- any text you put into the multi-line editor will be added to `msg.payload` with the newlines intact. And since SQL statements can contain newlines, this should just work without using any javascript code.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/1012f594e762db8d3e412184cf0ec012bd588e26.png)

Please note that this is NOT the `ui_template` node... and if you want to insert incoming msg values into your text, it also support moustache (sic ;\*) syntax like `{{payload.tablename}}`.

---

<div class="post-metadata">

### Author: ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)
#### Post date: [19 December 2023 08:44 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/6 "2023-12-19T08:44:29Z")

</div>

Thanks shrickus

That is even better, no back ticks to worry about, I am going to use mustache 🙂

---

<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: [19 December 2023 10:09 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/7 "2023-12-19T10:09:26Z")

</div>

And you can set the **Syntax Highlight** dropdown to SQL 🕶

---

<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: [19 December 2023 12:03 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/8 "2023-12-19T12:03:10Z")

</div>

> [@shrickus](#):
>
> support moustache

I for one support all rodent facial hair  
 ![bustedtees_funny-mous-tache-mouse-pun-bustedtees_1572873117.large](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08b452908763e9f4522db923cec1fbb5cd553d4e.png)

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [19 December 2023 14:11 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/9 "2023-12-19T14:11:20Z")

</div>

> [@questuk](#):
>
> no back ticks to worry about

True - what you see is what you get... but if you do use "mustache" syntax to insert data into that DDL string, don't forget to use the triple braces i.e. `{{{payload.tablename}}}` to stop any potential html character encoding!

---

<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: [17 February 2024 14:11 UTC](https://discourse.nodered.org/t/is-there-a-neater-way-of-creating-this-payload/83809/10 "2024-02-17T14:11:37Z")

</div>

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