# JSONata function tutorial WIP

**URL:** <https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086>\
**Category:** General\
**Created:** [19 March 2022 19:14 UTC](https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086 "2022-03-19T19:14:11Z")\
**Posts on this page:** 4\
**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:** [19 March 2022 19:14 UTC](https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086/1 "2022-03-19T19:14:11Z")

</div>

In the process of leaning JSONata, I've been creating a tutorial of their Function Library seen in the sidebar here: [JSONata Documentation · JSONata](https://docs.jsonata.org/overview.html) using the npm module [jsonata - npm](https://www.npmjs.com/package/jsonata)

However, I seem to be having a bit of difficulty with two functions in particular. `$error` and `$assert`

In the first function node I have:

```auto
// $error(message)

// THIS IS A HACKY WORKAROUND
node.error(jsonata("$error('Error 12345')").ast().arguments[0].value);

// msg.payload = jsonata("$error('Error 12345')").evaluate();

// return msg;

```

The workaround technically works, but seems like a bad way to go about it. The lower method only produces `error "[object Object]"`, is there a way to use the `message` property in Node-RED as seen in the JSONata documentation?

In the second function I have :

```auto
// $assert(condition, message)

// If condition is true, the function returns undefined.
// If condition is false, an exception is thrown with the message as the message of the exception.
const expression = jsonata(`$assert(${msg.payload} % 2 = 0)`);

//const expression = jsonata("$assert($a % 2 = 0, 'The number must be even')");
//expression.assign("a", msg.payload);

msg.payload = expression.evaluate();

return msg;

```

Both methods only produces `error "[object Object]"` when false rather than the error message (if exists), any ideas?

---

<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:** [20 March 2022 09:26 UTC](https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086/2 "2022-03-20T09:26:58Z")

</div>

You are getting a object you just need to find the property names returned, Use `Object.entries()` to see exactly what the object is, or or `.ast().arguments[0].name` to see the values property name. I suspect that the object property you want is `.message`  
I am liitle confused as to where the error "[object Object]" is being displayed/returned.

---

<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:** [20 March 2022 14:29 UTC](https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086/3 "2022-03-20T14:29:56Z")

</div>

So it appears that the values property name is `undefined`, and `.message` does not exist.

Maybe my 'HACKY WORKAROUND' isn't so bad after all.

I appreciate your assistance. It lead me to this for `$assert`:

```auto
// $assert(condition, message)

// If condition is true, the function returns { "payload": "assertation is true" }.
// If condition is false, an error is thrown, with the message as the message of the error.
msg.payload = jsonata(`$assert(${msg.payload} % 2 = 0, "Number must be even")`);

// HACKY WORKAROUND for message parameter to be usable
// 'if condition' must be relevant to 'assert condition'
if (msg.payload.ast().arguments[0].lhs.lhs.value % 2 == 0) return { "payload": "assertation is true" };
else node.error(`${msg.payload.ast().arguments[1].value}`);

```

Which is probably as good as it's going to get. Thanks again.

---

<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:** [19 May 2022 14:29 UTC](https://discourse.nodered.org/t/jsonata-function-tutorial-wip/60086/4 "2022-05-19T14:29:56Z")

</div>

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