Put a variable data into Json code?

#1

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 ?


.
.
.
I then want to place the Electricity reading of 0.002, into Json code below
.

the above code does work in a graph.

.
.
.
How do I do this ?

Apologies if I have the terminology wrong, still learning :wink:

#2

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?

#3

Hi nlecaude,

I am not sure, but the output from

this is it in the debug window

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

#4

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):

[{
	"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...

1 Like
#5

Hi shrickus

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