How store JSONata expression inside a global variable and use that for evaluation


#1

I storing the JSONata expression inside Database or variable and while evaluating on incoming message how to use that expression(Which is stored as just string).

How to achieve this in node-red.


#2

I think the only way is to npm install the jsonata library into your project, require it in your settings.js file, and call it from inside your function code.

Andre shows the process (for another lib) at the start of this discussion -- good luck!


#3

Thank you!! , is there any example like as the expression is stored as text/string in database how can I use that to evaluate in function node.


#4

I'm assuming you already have the expression selected from the database? You will need to send a msg with both the payload data and the expression into your function so you can call the jsonata library functions.


#5

I think the new version of JSONata as an $eval method... Could be useful for that.


#6

Just tested and indeed this is available in 0.20.x

[{"id":"40842da7.426894","type":"change","z":"cfec421b.651ef","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$eval(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":220,"wires":[["6200981c.e48d18"]]},{"id":"4405e8d6.23911","type":"inject","z":"cfec421b.651ef","name":"","topic":"","payload":"2 + 2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":220,"wires":[["40842da7.426894"]]},{"id":"6200981c.e48d18","type":"debug","z":"cfec421b.651ef","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":940,"y":220,"wires":[]}]