RED.util.prepareJSONataExpression and RED.util.evaluateJSONataExpression

235 views
Skip to first unread message

skavan

unread,
Apr 23, 2018, 1:17:09 PM4/23/18
to Node-RED
Hi,

I have written a custom node and in my test harness, with jsonata installed, it works fine, a simplified example is here:

expression = jsonata(`{"topic": $jaTopic, "retain": true, "delay": 0,
               "payload" : $map($lookup(valuemaps,$jaCommand),
                function($v, $i, $a){{"displayValue": $v.displayValue, "command" : $v.standardValue[0] }})}`);

expression.assign('jaTopic', var1);
expression.assign('jaCommand', var2);
return expression.evaluate(jsonData);


Now, as I head over to my node-red (rpi) test-bed, I belatedly notice that the approach used by other nodes is to use:
RED.util.prepareJSONataExpression

and

RED.util.evaluateJSONataExpression

So, I guess I have two questions:
1. Should I avoid npm'ing a jsonata install for my own nodes.
2. How do I map the code above into the RED.util functions? I can't find any docs!

Thanks.



Nick O'Leary

unread,
Apr 23, 2018, 3:14:31 PM4/23/18
to Node-RED Mailing List
Hi,

you can see the definition of these two functions here: https://github.com/node-red/node-red/blob/master/red/runtime/util.js#L335

The prepareJSONataExpression does a couple extra things:
 - it defines the $flowContext and $globalContext functions that allow an expression to lookup values from context
 - it provides a custom clone function - needed as JSONata's native clone function cannot clone some node.js types (as its meant for pure JSON). This is needed in particular when used in a flow that starts with HTTP In as the msg.req and msg.res objects are not cloneable.
 - it also assumes you are going to be evaluating 

RED.util.prepareJSONataExpression is pretty much a drop-in replacement for the jsonata function. It does take a second argument, the node evaluating the expression if you want to use the flowContext/globalContext functions.


    expression = RED.util.prepareJSONataExpression(`{"topic": $jaTopic, "retain": true, "delay": 0,
                "payload" : $map($lookup(valuemaps,$jaCommand), 
                function($v, $i, $a){{"displayValue": $v.displayValue, "command" : $v.standardValue[0] }})}`);
    expression.assign('jaTopic', var1);
    expression.assign('jaCommand', var2);          
    return RED.util.evaluateJSONataExpression(expression,jsonData);


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/e0e69d76-8258-4779-a0d6-45cec36bbbda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

skavan

unread,
Apr 23, 2018, 7:19:50 PM4/23/18
to Node-RED
Thanks. Super helpful.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages