# Node-red Json to database writing

**URL:** <https://discourse.nodered.org/t/node-red-json-to-database-writing/57523>\
**Category:** General\
**Created:** [30 January 2022 22:05 UTC](https://discourse.nodered.org/t/node-red-json-to-database-writing/57523 "2022-01-30T22:05:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamedaminhamdad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mohamedaminhamdad/32/55417_2.png) [@Mohamedaminhamdad](https://discourse.nodered.org/u/Mohamedaminhamdad)\
**Post date:** [30 January 2022 22:05 UTC](https://discourse.nodered.org/t/node-red-json-to-database-writing/57523/1 "2022-01-30T22:05:47Z")

</div>

Hello everybody,  
i am a newbie in node-red. I want to use it to write a json file into a PostgreSQL database, where the json file should be divided into different tables and written in a database. Is there an easy way to do that?

example json file:  
{ 'location': 100 ,  
'job\_id': 1,  
'operator': 'Jenny',  
'curve': {'time\_series':[1,2,3,4,5,6], 'Momentum':[1,2,3,4,5,6]},  
}  
I want a table of:  
data with columns location, job\_id, operator curve and another table named curve with columns time\_series and Momentum, is that possible with Node-Red.

---

<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:** [30 January 2022 22:44 UTC](https://discourse.nodered.org/t/node-red-json-to-database-writing/57523/2 "2022-01-30T22:44:35Z")

</div>

> [@Mohamedaminhamdad](#):
>
> is that possible with Node-Red.

Most definitely.

Something to get you started...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d24332bf162ae08073024ba149913c00587d86e3.png)

demo flow (use CTRL+I to import)

```auto
[{"id":"9358d3d721f4b18f","type":"inject","z":"85e1791bc0cd6285","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"location\":100,\"job_id\":1,\"operator\":\"Jenny\",\"curve\":{\"time_series\":[1,2,3,4,5,6],\"Momentum\":[1,2,3,4,5,6]}}","payloadType":"json","x":2330,"y":1282,"wires":[["e974f54d76d72765"]]},{"id":"e974f54d76d72765","type":"change","z":"85e1791bc0cd6285","name":"","rules":[{"t":"move","p":"payload.curve","pt":"msg","to":"curve","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2362,"y":1334,"wires":[["4feffe5c5fd46429","e4936e949e7d4801"]]},{"id":"4feffe5c5fd46429","type":"function","z":"85e1791bc0cd6285","name":"Built insert Query","func":"msg.payload = `INSERT INTO t1 (location, job_id, operator)\nVALUES (${msg.payload.location}, ${msg.payload.job_id}, '${msg.payload.operator}')`\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2632,"y":1334,"wires":[["ab0bcbd8a6aff070"]]},{"id":"e4936e949e7d4801","type":"function","z":"85e1791bc0cd6285","name":"Built insert Query2","func":"\nconst ts = JSON.stringify(msg.curve.time_series)\nconst mom = JSON.stringify(msg.curve.Momentum)\nmsg.payload = `INSERT INTO t2 (time_series, Momentum)\nVALUES ($'{ts}', '${mom}')`\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2632,"y":1394,"wires":[["4666a71a77e9d22a"]]},{"id":"ab0bcbd8a6aff070","type":"debug","z":"85e1791bc0cd6285","name":"To Database →","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2862,"y":1334,"wires":[]},{"id":"4666a71a77e9d22a","type":"debug","z":"85e1791bc0cd6285","name":"To Database →","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2862,"y":1394,"wires":[]}]

```

* * *

### PS

in order to make code readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

* * *

### PS2

As a new user, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [31 March 2022 22:45 UTC](https://discourse.nodered.org/t/node-red-json-to-database-writing/57523/3 "2022-03-31T22:45:18Z")

</div>

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