# Trigger node does NOT support JSONata on ouptput messages

**URL:** <https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436>\
**Category:** Feature Requests\
**Created:** [13 December 2020 02:53 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436 "2020-12-13T02:53:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![zinahe](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zinahe](https://discourse.nodered.org/u/zinahe)\
**Post date:** [13 December 2020 02:53 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/1 "2020-12-13T02:53:24Z")

</div>

Hi All,

A very happy user of Node-RED here. Thank you all for the hard work.

I have noticed that the Trigger node is missing JSONata (J: Expression) option to compose output messages. I ran in to an issue where the output message(s) depended on some values from the incoming **msg.payload**.

I understand that there could be workarounds to achieve what I'm looking for. But I'm wondering if this was an intentional design decision

Regards,

---

<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:** [13 December 2020 10:56 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/2 "2020-12-13T10:56:31Z")

</div>

Hi, indeed - at the time of creation JSONate was probably not anywhere apart from the change node. As you suggest it's not hard to add a change node to follow to achieve the same. It could certainly be added if anyone felt strongly enough to offer up a Pull Request, or we can add it to the backlog.

---

<div class="post-metadata">

**Author:** ![zinahe](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zinahe](https://discourse.nodered.org/u/zinahe)\
**Post date:** [13 December 2020 13:36 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/3 "2020-12-13T13:36:52Z")

</div>

@dceejay,

Thank you for the quick response. I have never delved deep in to the source of Node-RED, but if you could kindly give some pointers about where and what to look for; I'd be happy to give it a shot.

In the meantime, for anyone who is looking for a quick way out of a similar situation, I got it done using a function node with the following code:

```javascript
var first_msg = { payload: msg.payload + "_first" };
node.send(first_msg);

var delayed_msg = { payload:msg.payload + "_delayed" };
setTimeout(function() {
    node.send(delayed_msg);
}, 500);

```

Whoever came up with the idea of the function node, God bless him/her!

---

<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:** [13 December 2020 13:52 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/4 "2020-12-13T13:52:56Z")

</div>

You could have done it with the standard Trigger node telling it to send the two messages to different outputs, with a Change node on each, adding the extra text. However, if your function node does what you want then that is ok, obviously.

---

<div class="post-metadata">

**Author:** ![zinahe](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zinahe](https://discourse.nodered.org/u/zinahe)\
**Post date:** [13 December 2020 14:26 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/5 "2020-12-13T14:26:03Z")

</div>

@Colin ,

You are absolutely right.

Even better, a Trigger node with J: expression capability would have sufficed by itself. I don't know if I have some un-diagnosed OCD or what but using more nodes than needed irks me to no end. Which is why I made the suggestion in the op, and also ended up using a single function node.

Cheers,

---

<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:** [13 December 2020 14:54 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/6 "2020-12-13T14:54:35Z")

</div>

> [@zinahe](#):
>
> a Trigger node with J: expression capability would have sufficed by itself

Certainly.  
I tend to avoid Function nodes unless they save a significant number of core nodes. There is a small overhead in function nodes compared to 'real' nodes, but more significantly any time one is writing code there is a much greater chance of unnoticed bugs creeping in. For example, have you considered what will happen if you get another message arriving at the function before the second original message has been sent?

---

<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:** [13 December 2020 16:27 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/7 "2020-12-13T16:27:26Z")

</div>

@colin - yes there is a small overhead - but there is also an overhead when calling JSONata, so unless you are doing 100's of messages a seconds - don't sweat it.

But yes - good point re subsequent messages - as the JSONata needs to be evaluated should that be done immediately on the original message values - or at the point of sending the second message at which point the values in the last arriving message may be used. I tend to think it should probably be the latest/most current values that get used.

---

<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:** [13 December 2020 16:39 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/8 "2020-12-13T16:39:28Z")

</div>

> [@dceejay](#):
>
> good point re subsequent messages

I was specifically meaning with the function suggested, which will have unexpected results in that situation I think.  
However that is a good point wrt adding J: to the output options as it brings in a completely new concept to the node. Currently, for the second message one can only output a pre configured message, the first message, or the latest. With J: one has the additional option of using a modified version of the input message. For full flexibility I think one would need the option of applying this to either the first or the latest. As is so often the case an apparently simple enhancement gets more complex as one looks at it in detail.

---

<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:** [13 December 2020 16:57 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/9 "2020-12-13T16:57:45Z")

</div>

I think if implemented it would be on latest value only. As JSONata has hooks into so many other things like context and time then I would expect all those things to have possibly changed at the instant of sending.. eg if I wanted to append a timestamp or add a constant factor from context etc.

Alternatively this is one of those cases where two nodes are better than one 🙂

---

<div class="post-metadata">

**Author:** ![zinahe](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zinahe](https://discourse.nodered.org/u/zinahe)\
**Post date:** [14 December 2020 15:41 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/10 "2020-12-14T15:41:01Z")

</div>

> [@Colin](#):
>
> For example, have you considered what will happen if you get another message arriving at the function before the second original message has been sent?

@Colin, Good catch!

I did not think about the scenario where a preemptive message arrives. This should fix it. Now, the function returns immediately with the content of the second message getting composed and passed as a parameter to `setTimeout` as quickly as possible. Still not bullet proof, but should take care of 99% of the cases where microsecond-level resolution is not needed.

```javascript
var first_msg = { payload: msg.payload + "_first" };
var delayed_msg = { payload:msg.payload + "_delayed" };

node.send(first_msg);
setTimeout(node.send, 500, delayed_msg);

```

But, I do agree that there is a risk of such kind of subtle details remaining buried inside of function nodes.

---

<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:** [14 December 2020 16:06 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/11 "2020-12-14T16:06:38Z")

</div>

I don't see how this differs in operation to the first version. node.send() returns immediately and will start the timeout. The problem (if it is a problem) is that if you send in two messages close together then the output will be the \_first response for the first message, followed by the \_first for the second message, followed by \_delayed for the first and \_delayed for the second.  
This all goes to show the reason that such functions are best avoided if possible, there are many traps for the uninitiated (and the initiated) to fall into. Much better where reasonable to use core nodes that have been tested extensively and should give the expected results.

---

<div class="post-metadata">

**Author:** ![zinahe](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zinahe](https://discourse.nodered.org/u/zinahe)\
**Post date:** [17 December 2020 02:08 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/12 "2020-12-17T02:08:00Z")

</div>

Well, logically that is what is supposed to happen; as long as the delayed message of the of the first message is not overwritten by the preempting one. The fix I was referring to was to protect the content of the \_delayed response of the initial message not to be overwritten by second.

---

<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:** [17 December 2020 09:24 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/13 "2020-12-17T09:24:04Z")

</div>

> [@zinahe](#):
>
> Well, logically that is what is supposed to happen

That depends on what the application is. That is not what the Trigger node does.

> [@zinahe](#):
>
> The fix I was referring to was to protect the content of the \_delayed response of the initial message not to be overwritten by second.

That won't happen, each message will get its own set of local variables (first\_msg and delayed\_msg). That is why you have to use node context if you want data to be passed between different invocations of the function node.

---

<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:** [15 February 2021 09:24 UTC](https://discourse.nodered.org/t/trigger-node-does-not-support-jsonata-on-ouptput-messages/37436/14 "2021-02-15T09:24:16Z")

</div>

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