Use of payload in Jsonata expression

1,586 views
Skip to first unread message

Bart Butenaers

unread,
Jul 16, 2017, 6:45:28 PM7/16/17
to Node-RED
Hi everybody,

I started with my first Jsonata expressions today, but I'm currently struggling with the use of msg.payload inside my expressions.

First I have stored an array of Element objects in variable 'someGlobal' on the global context:

[{"id":"9f9d3bb8.b2e348","type":"inject","z":"d0d4f76e.209b68","name":"","topic":"","payload":"{\"Element\":[{\"name\":\"element1\"},{\"name\":\"element2\"},{\"name\":\"element3\"}]}","payloadType":"json","repeat":"","crontab":"","once":false,"x":850,"y":740,"wires":[["e6bb1904.842c78"]]},{"id":"e6bb1904.842c78","type":"change","z":"d0d4f76e.209b68","name":"Get scenario topics","rules":[{"t":"set","p":"someGlobal","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1050,"y":740,"wires":[[]]}]

With the context browser you can see the result: I have a 'someGlobal' variable containing an array of 3 Element's (each with their own name).


Now I want to read the Element with name = 'element1' from the array, using a Jsonata expression (by injecting a payload string containing 'element1'):



[{"id":"81465335.1ee9d","type":"debug","z":"d0d4f76e.209b68","name":"Test 3","active":true,"console":"false","complete":"payload","x":930,"y":640,"wires":[]},{"id":"11f58507.7a5a3b","type":"inject","z":"d0d4f76e.209b68","name":"","topic":"","payload":"element1","payloadType":"str","repeat":"","crontab":"","once":false,"x":440,"y":440,"wires":[["891f4e86.2f6c3","bc086635.acdd88","b39b0120.4082c"]]},{"id":"b39b0120.4082c","type":"change","z":"d0d4f76e.209b68","name":"Get element by hardcoded name","rules":[{"t":"set","p":"payload","pt":"msg","to":"$globalContext(\"someGlobal\").Element[name=payload]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":640,"wires":[["81465335.1ee9d"]]},{"id":"efb67387.f475d","type":"debug","z":"d0d4f76e.209b68","name":"Test 2","active":true,"console":"false","complete":"payload","x":930,"y":540,"wires":[]},{"id":"bc086635.acdd88","type":"change","z":"d0d4f76e.209b68","name":"Get element by name in payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"$globalContext(\"someGlobal\").Element[name='element1']","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":540,"wires":[["efb67387.f475d"]]},{"id":"891f4e86.2f6c3","type":"change","z":"d0d4f76e.209b68","name":"Get name in payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":440,"wires":[["ee9e7b40.25ebb8"]]},{"id":"ee9e7b40.25ebb8","type":"debug","z":"d0d4f76e.209b68","name":"Test 1","active":true,"console":"false","complete":"payload","x":930,"y":440,"wires":[]},{"id":"755ed4c5.e1948c","type":"comment","z":"d0d4f76e.209b68","name":"payload","info":"","x":610,"y":400,"wires":[]},{"id":"f5f1cfa0.df0d8","type":"comment","z":"d0d4f76e.209b68","name":"$globalContext(\"someGlobal\").Element[name='element1']","info":"","x":770,"y":500,"wires":[]},{"id":"cda74f2d.75d07","type":"comment","z":"d0d4f76e.209b68","name":"$globalContext(\"someGlobal\").Element[name=payload]","info":"","x":760,"y":600,"wires":[]}]

  • Test 1 : The Jsonata expression is 'payload' and indeed the payload 'element1' is displayed.  So that is Ok!  Jsonata recognizes the payload keyword.
  • Test 2 : The Jsonata expression contains hardcoded the Element name 'element1' and it gets the first Element from the array.  So that is also Ok!
  • Test 3 : The Jsonata expression should again get the first Element, but now based on the name 'element1' from the msg.payload.  But that is not OK. It cannot find any of the elements ...
I've tried a lot of constructions, but nothing solves my issue.  Does anybody knows what I'm doing wrong, because it's still a black box te me.

Thanks in advance !!
Bart Butenaers

Zenofmud

unread,
Jul 16, 2017, 6:54:07 PM7/16/17
to node...@googlegroups.com
Your second flow gives me errors for test2 and test3
7/16/2017, 6:49:35 PMnode: Test 1msg : Object
_msgid"e9588fdf.4b463"topic""payload"element1" }
7/16/2017, 6:49:35 PMnode: Get element by name in payloadmsg : string[70]
"Invalid JSONata expression: node.context(...).global is not a function"
7/16/2017, 6:49:35 PMnode: Get element by hardcoded namemsg : string[70]
"Invalid JSONata expression: node.context(...).global is not a function"

Bart Butenaers

unread,
Jul 17, 2017, 2:16:12 AM7/17/17
to Node-RED
Hi Paul,

thanks for having a look at my issue!  I'm running the latest Node-Red version 0.17.4.  

I assume you are running an older version, because in the release notes you can see that your error has been fixed in that version:


Kind regards,

Bart


Bart Butenaers

unread,
Jul 17, 2017, 2:31:21 AM7/17/17
to Node-RED
Hi Paul,

when having a closer look at that fix 1335, thinks become even more obscure to me.

While preparing the Jsonata expression, it seems that Node-Red assigns two functions (to support both for $flowcontext and $globalcontext):

I would expect here some coding for payload also, but that is probably somewhere else ...
P.S. I also tried $msg.payload, msg.payload, $payload, ...  all without success ;-(

Bart

Nick O'Leary

unread,
Jul 17, 2017, 5:44:04 AM7/17/17
to Node-RED Mailing List
Bart,

the expression you want is:
    $globalContext("someGlobal").Element[name=$$.payload]

You need the $$ to access the original root message.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/05b246b0-93d8-42fa-ab23-2f406ff79b75%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bart Butenaers

unread,
Jul 17, 2017, 6:01:32 AM7/17/17
to node...@googlegroups.com
Hey Nick,

about an hour ago I indeed found that the msg is the root object:


So I also tried the $$.payload, but without success.  The payload of my output message remains 'undefined'.​..
I assume at this point, that it will be something very stupid I'm doing wrong ...

Thanks !!!
Bart

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XsFspG-1p_k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nick O'Leary

unread,
Jul 17, 2017, 6:03:04 AM7/17/17
to Node-RED Mailing List
Hi Bart, that expression is working for me.

[{"id":"6fbbc3e7.9c8f6c","type":"inject","z":"d99f85d8.df6088","name":"","topic":"","payload":"element1","payloadType":"str","repeat":"","crontab":"","once":false,"x":106,"y":95,"wires":[["675c4744.a14928","c950a37d.4d3c","baa832e5.d161b"]]},{"id":"675c4744.a14928","type":"change","z":"d99f85d8.df6088","name":"Get name in payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":326,"y":95,"wires":[["e4749b89.f258e8"]]},{"id":"c950a37d.4d3c","type":"change","z":"d99f85d8.df6088","name":"Get element by name in payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"$globalContext(\"someGlobal\").Element[name='element1']","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":356,"y":195,"wires":[["c07684e3.bf38c8"]]},{"id":"baa832e5.d161b","type":"change","z":"d99f85d8.df6088","name":"Get element by hardcoded name","rules":[{"t":"set","p":"payload","pt":"msg","to":"$globalContext(\"someGlobal\").Element[name=$$.payload]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":366,"y":295,"wires":[["41e470a6.f843a"]]},{"id":"e4749b89.f258e8","type":"debug","z":"d99f85d8.df6088","name":"Test 1","active":true,"console":"false","complete":"payload","x":596,"y":95,"wires":[]},{"id":"c07684e3.bf38c8","type":"debug","z":"d99f85d8.df6088","name":"Test 2","active":true,"console":"false","complete":"payload","x":596,"y":195,"wires":[]},{"id":"41e470a6.f843a","type":"debug","z":"d99f85d8.df6088","name":"Test 3","active":true,"console":"false","complete":"payload","x":596,"y":295,"wires":[]}]


Nick

Bart Butenaers

unread,
Jul 17, 2017, 6:09:37 AM7/17/17
to Node-RED
Nick,

I promised you I was doing something stupid ..;
I forgot to inject the array again into my global context in my last test.

Now it indeed works perfectly with $$.payload.  

Thanks a lot !!!!!!!!!!!!!!!!

Greetings from a rainy Belgium,
Bart
Reply all
Reply to author
Forward
0 new messages