How to send JSON out of function node?

hi all.. I am using this subflow to try and fade between values..

https://flows.nodered.org/flow/4abee421817eac7335b5633f166295af

i noticed the inject node in the subflows example sends JSON as the payload with a bunch of values.. which looks like this..

msg.topic: "start"
msg.payload:
{
    "initialdelay": 2,
    "start": 0,
    "end": 1,
    "duration": 3,
    "step": 0.2,
    "unit": "Seconds"
}

i am wondering how to send JSON data like this out of a function node but have the "start" and "end" values change based on a variable that is in flow context ? i know how to take the data from flow context and put it in a variable in the function node.. then i can send that variable out as regular msg.payload... but how do i get the variable to become the "start" or "end" value inside the JSON string and then send the JSON out of the function node ? hope that makes sense..

I assume you mean that you want to send a javascript object, rather then JSON which is a string. If you have the values in a variable x and y, for example then in the function node you can use

msg.payload = {}           // create an empty object
msg.payload.start = x
msg.payload.end = y

If the payload is already an object then you don't need to recreate it of course so you can leave out the first lines.
Also you do not need to fetch it to a variable first, you can just use flow.get() in the assignment line if you want to.

fantastic thanks for your help Colin.. its now working perfectly, and i understand how to adapt it further as needed.. much appreciated..

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