# JSONata in function node

**URL:** <https://discourse.nodered.org/t/jsonata-in-function-node/59772>\
**Category:** General\
**Created:** [12 March 2022 20:51 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772 "2022-03-12T20:51:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [12 March 2022 20:51 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/1 "2022-03-12T20:51:42Z")

</div>

In the process of learning JSONata I'm having difficulty figuring out how to utilize regex as a parameter. Take for example the following in a `function` node:

```auto
const example = "ababbabbcc";
// Applies the string to the pattern regular expression and returns an array of objects
const expression = jsonata(`$match(${example}, /a(b+)/)`);

msg.payload = expression.evaluate();
return msg;

```

I am under the impression this does not work, due to the regex being wrapped in backticks, thus becoming a string.

How can I go about achieving a result such as:

```auto
[
  {
    "match": "ab",
    "index": 0,
    "groups": ["b"]
  },
  {
    "match": "abb",
    "index": 2,
    "groups": ["bb"]
  },
  {
    "match": "abb",
    "index": 5,
    "groups": ["bb"]
  }
]

```

As seen in the JSONata documentation here: [String functions · JSONata](https://docs.jsonata.org/string-functions)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [12 March 2022 22:36 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/2 "2022-03-12T22:36:30Z")

</div>

I wasn't aware that jsonata functions were available to use in a function node??

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [12 March 2022 22:41 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/3 "2022-03-12T22:41:16Z")

</div>

To clarify, I am using the external module [jsonata - npm](https://www.npmjs.com/package/jsonata)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 March 2022 23:35 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/4 "2022-03-12T23:35:34Z")

</div>

I have not tested, but as example is a string it would have to have quotes around it, to form a correct expression. The expression is therefore looking for a property in an object, called `example`, but you did not feed it any data when you evaluated.

```auto
const expression = jsonata(`$match("${example}", /a(b+)/)`);

```

but probably better to do

```auto
let data = {
  example: "ababbabbcc"
}
const expression = jsonata('$match(example, /a(b+)/)');
msg.payload = expression.evaluate(data);
return msg;

```

Again untested.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [13 March 2022 14:07 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/5 "2022-03-13T14:07:36Z")

</div>

This worked, thank you for your help.

---

<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:** [27 March 2022 14:22 UTC](https://discourse.nodered.org/t/jsonata-in-function-node/59772/7 "2022-03-27T14:22:11Z")

</div>

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