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

**URL:** <https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032>\
**Category:** General\
**Created:** [15 March 2019 18:34 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032 "2019-03-15T18:34:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![VIKRAMAS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikramas/32/3955_2.png) [@VIKRAMAS](https://discourse.nodered.org/u/VIKRAMAS)\
**Post date:** [15 March 2019 18:34 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/1 "2019-03-15T18:34:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [15 March 2019 19:01 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/2 "2019-03-15T19:01:10Z")

</div>

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](https://discourse.nodered.org/t/flow-to-decode-binary-data-and-a-walk-through-on-how-to-install-the-npm-module/5174) -- good luck!

---

<div class="post-metadata">

**Author:** ![VIKRAMAS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikramas/32/3955_2.png) [@VIKRAMAS](https://discourse.nodered.org/u/VIKRAMAS)\
**Post date:** [15 March 2019 19:04 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/3 "2019-03-15T19:04:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [15 March 2019 19:07 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/4 "2019-03-15T19:07:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [16 March 2019 16:03 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/5 "2019-03-16T16:03:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [16 March 2019 17:09 UTC](https://discourse.nodered.org/t/how-store-jsonata-expression-inside-a-global-variable-and-use-that-for-evaluation/9032/6 "2019-03-16T17:09:17Z")

</div>

Just tested and indeed this is available in 0.20.x

```auto
[{"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":[]}]

```
