# How to send JSON out of function node?

**URL:** https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500
**Category:** General
**Created:** [9 September 2022 02:42 UTC](https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500 "2022-09-09T02:42:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![EaB11](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@EaB11](https://discourse.nodered.org/u/EaB11)
#### Post date: [9 September 2022 02:42 UTC](https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500/1 "2022-09-09T02:42:34Z")

</div>

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

[https://flows.nodered.org/flow/4abee421817eac7335b5633f166295af](https://flows.nodered.org/flow/4abee421817eac7335b5633f166295af)

> **[Fade in out · cflurin/node-red-contrib-dsm Wiki](https://github.com/cflurin/node-red-contrib-dsm/wiki/Fade-in--out)**
>
> A dynamic state machine node based on the Finite state machine. - Fade in out · cflurin/node-red-contrib-dsm Wiki

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

```auto
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..

---

<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: [9 September 2022 07:03 UTC](https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500/2 "2022-09-09T07:03:10Z")

</div>

> [@EaB11](#):
>
> 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 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

```auto
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.

---

<div class="post-metadata">

### Author: ![EaB11](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@EaB11](https://discourse.nodered.org/u/EaB11)
#### Post date: [9 September 2022 08:52 UTC](https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500/3 "2022-09-09T08:52:33Z")

</div>

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

---

<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: [23 September 2022 08:53 UTC](https://discourse.nodered.org/t/how-to-send-json-out-of-function-node/67500/4 "2022-09-23T08:53:13Z")

</div>

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