# Put a variable data into Json code?

**URL:** https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266
**Category:** General
**Created:** [28 November 2018 14:26 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266 "2018-11-28T14:26:05Z")
**Posts on this page:** 5
**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: [28 November 2018 14:26 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266/1 "2018-11-28T14:26:05Z")

</div>

Hi,

My aim is to read from my Sqlite database and eventually output to a Node-RED graph.

.  
I have this coming out of my database, I think it's an array ?

 ![13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9b38d0fdb9816bae65b230bd2c14cb697fa7243e.jpeg)  
.  
.  
.  
I then want to place the **Electricity** reading of 0.002, into Json code below  
.  
 ![14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/ab9d465a89dc2cb55226642b298f9e60c3173054.jpeg)  
the above code does work in a graph.

.  
.  
.  
How do I do this ?

Apologies if I have the terminology wrong, still learning 😉

---

<div class="post-metadata">

### Author: ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)
#### Post date: [28 November 2018 18:02 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266/2 "2018-11-28T18:02:31Z")

</div>

Should each element in the incoming array appear as individual elements in your output JSON ? In your example you have an array of 5 values, should this translate to 5 values in the data property?

---

<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: [28 November 2018 18:23 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266/3 "2018-11-28T18:23:43Z")

</div>

Hi nlecaude,

I am not sure, but the output from

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

this is it in the debug window  
 ![18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3344113c35e831cec00307836da0bb80308f3e69.jpeg)

So this is what I need to recreate, then this will populate a Node-RED graph.

I must admit I have read so many things, I am not sure if I need Javascript output or JSONata or JSON ?  
All new to me, but i have tried to find out just need some help !

Thanks

---

<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: [28 November 2018 20:37 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266/4 "2018-11-28T20:37:05Z")

</div>

> [@questuk](#):
>
> I have this coming out of my database, I think it's an array ?

Yes, `msg.payload` is an array of 5 records.

So step #1 is to hover the mouse over that top line that says `object` in the debug sidebar -- when the buttons appear on the right, use the second one to "copy" the value of that object to the clipboard.

Step #2: add a `change` node downstream from your query node, "Set" `msg.payload` "to" `J:` and click the ellipsis (`...`) next to the expression text box. In the expression editor, paste that msg object into the bottom-left window (overwriting the default data that is shown).

Step #3: now you can write a JSONata expression that will convert your query results array into the data you need for the charts -- something like this should be close (paste it into the upper window):

```auto
[{
	"series": ["Electricity"],
    "labels": ["kWh"],
    "data": [[$$.payload.{
    	"x": $fromMillis(Unix_Time*1000),
        "y": Electricity
    }]]
}]

```

Step #4: stare with open-mouth as your chart data appears in the bottom-left output window ;\*)

Step #5: save the `change` node settings, and wire it to your chart node...

---

<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: [28 November 2018 20:51 UTC](https://discourse.nodered.org/t/put-a-variable-data-into-json-code/5266/5 "2018-11-28T20:51:12Z")

</div>

Hi shrickus

Thanks for that, getting late here now, will try in the morning !
