# Javascript function block help

**URL:** https://discourse.nodered.org/t/javascript-function-block-help/50618
**Category:** General
**Created:** [4 September 2021 19:30 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618 "2021-09-04T19:30:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bbartling](https://avatars.discourse-cdn.com/v4/letter/b/bcef8e/32.png) [@bbartling](https://discourse.nodered.org/u/bbartling)
#### Post date: [4 September 2021 19:30 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/1 "2021-09-04T19:30:28Z")

</div>

Would someone have a tip for how to change a `msg.payload`?

For example the `msg.payload` going into the function block is Boolean True or False. Can I modify the payload to something different?

```auto
var payload = msg.payload

if (payload==True){
    
    // JSON PAYLOAD FOR MOTOR ON
    
    msg.payload = {
    "address": "12345:2",
    "object_type": "analogValue",
    "object_instance": "302",
    "value": "100",
    "priority": "11"
    }
}

if (payload==False){

    // JSON PAYLOAD FOR MOTOR OFF
    
    msg.payload = {
    "address": "12345:2",
    "object_type": "analogValue",
    "object_instance": "302",
    "value": "0",
    "priority": "11"
    }

}

return msg;

```

Ultimately this needs to be JSON, would I then feed this output into a JSON block? Thanks for any tips

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [4 September 2021 19:33 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/2 "2021-09-04T19:33:03Z")

</div>

True is pyhton  
true is JavaScript  
You need javascript

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [4 September 2021 19:56 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/3 "2021-09-04T19:56:55Z")

</div>

> [@bbartling](#):
>
> Ultimately this needs to be JSON, would I then feed this output into a JSON block? Thanks for any tips

Yes if you return an object, No if you return a string in json format.

```auto
    // string in JSON format PAYLOAD FOR MOTOR ON
    
    msg.payload = `{
    "address": "12345:2",
    "object_type": "analogValue",
    "object_instance": "302",
    "value": "100",
    "priority": "11"
    }`

```

---

<div class="post-metadata">

### Author: ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)
#### Post date: [4 September 2021 21:13 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/4 "2021-09-04T21:13:40Z")

</div>

Isn't this topic about the same thing as your previous one? You started to ask the question there.

> [@UI scheduler node for different json payloads on HTTP requests](https://discourse.nodered.org/t/ui-scheduler-node-for-different-json-payloads-on-http-requests/50617):
>
> Can someone give me a tip on how to incorporate using the UI scheduler node to send out different different JSON payloads that get rolled into the body of an HTTP GET requests? The equipment that I am scheduling through ui scheduler handles rest API. For example when UI scheduler node is True from the schedule or the slider I need this JSON payload to start a motor: { "address": "12345:2", "object\_type": "analogValue", "object\_instance": "302", "value": "100", "priority": …

---

<div class="post-metadata">

### Author: ![bbartling](https://avatars.discourse-cdn.com/v4/letter/b/bcef8e/32.png) [@bbartling](https://discourse.nodered.org/u/bbartling)
#### Post date: [5 September 2021 12:25 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/5 "2021-09-05T12:25:29Z")

</div>

Yes, sorry newbie here. Not sure how ask good questions 🙄

---

<div class="post-metadata">

### Author: ![bbartling](https://avatars.discourse-cdn.com/v4/letter/b/bcef8e/32.png) [@bbartling](https://discourse.nodered.org/u/bbartling)
#### Post date: [5 September 2021 12:26 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/6 "2021-09-05T12:26:05Z")

</div>

thats make sense! Thanks for the Python insight I have learned that first now picking up javascript

---

<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: [4 November 2021 12:26 UTC](https://discourse.nodered.org/t/javascript-function-block-help/50618/7 "2021-11-04T12:26:49Z")

</div>

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