# JSONata evaluate expression inside function node is returning empty object at debug window

**URL:** <https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834>\
**Category:** General\
**Tags:** jsonata\
**Created:** [7 March 2025 04:57 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834 "2025-03-07T04:57:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 04:57 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/1 "2025-03-07T04:57:13Z")

</div>

Hi,

I can work with JSONATA expression with inject node and change node fine but I'm facing an issue while working with **JSONata** inside the **Function** node.

When I use JSONATA's .evaluate() method, i get empty object at the **debug window. Here’s the code snippet I’m using:**

```auto
let temp = []
temp[0] = jsonata(`$length(${arr[0]})`).evaluate();
temp[1] = jsonata(`$length(${arr[1]})`).evaluate();
temp[2] = jsonata(`$length(${arr[2]})`).evaluate();
temp[3] = jsonata(`$length(${arr[3]})`).evaluate();
temp[4] = jsonata(`$length(${arr[4]})`).evaluate();

msg.payload = temp;
return msg;

```

Here is the o/p i'm getting

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c94419cdc101680f09ff090771f7db03ecb96c4.png)

**Interestingly, when I use .ast() instead of .evaluate()**, it produces the expected output: Here is the expression i'm using for .ast() method:\*\*

```auto
let arr = ["He", "is", "a", "good", "boy"]

let temp = []
temp[0] = jsonata(`$length(${arr[0]})`).ast();
temp[1] = jsonata(`$length(${arr[1]})`).ast();
temp[2] = jsonata(`$length(${arr[2]})`).ast();
temp[3] = jsonata(`$length(${arr[3]})`).ast();
temp[4] = jsonata(`$length(${arr[4]})`).ast();

msg.payload = temp;
return msg;

```

**// ouput i'm getting:**

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/300aea480c577f57834f6052131b882f12cfebb7.png)

Could someone help me understand why **evaluate()** is returning an empty result, while **ast()** works correctly? Am I missing something in the way JSONata is being processed inside the **Function node**?

Any insights or suggestions would be greatly appreciated!

**Regards,**  
Shubham

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [7 March 2025 06:21 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/2 "2025-03-07T06:21:42Z")

</div>

Hi,

> [@shubhamnodered](#):
>
> while **ast()** works correctly?

`ast(..)` returns the [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) which is the internal representation of the expression passed to the `jsonata(...)` function the abstract syntax tree is used by the parser to evaluate the expression.

```auto
let arr = ["He", "is", "a", "good", "boy"]

let temp = []
temp[0] = jsonata(`$length(${arr[0]})`).ast();

```

because you're using backticks (`), the code is evaluate to `$length(He)` which for JSONata makes no sense since "He" isn't a variable name or anything else. If you want the length of a string, then you need to add quotes:

```auto
temp[0] = jsonata(`$length("${arr[0]}")`).evaluate()

```

that would evaluate to `$length("He")` which would JSONata would evalaute to 2.

---

<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:** [7 March 2025 08:34 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/3 "2025-03-07T08:34:25Z")

</div>

Just to note that node-red has some api modules to use JSONata in a function node.

> **[@node-red/util\_util - Documentation](https://nodered.org/docs/api/modules/v/1.0/@node-red_util_util.html#.evaluateJSONataExpression)**

> **[@node-red/util\_util - Documentation](https://nodered.org/docs/api/modules/v/1.0/@node-red_util_util.html#.evaluateJSONataExpression)**

```auto
let arr = ["He", "is", "a", "good", "boy"]
let expr = RED.util.prepareJSONataExpression("$$.$length($)", node);
 RED.util.evaluateJSONataExpression(expr, arr, function(err, output) {
        if (err) {
            node.warn(JSON.stringify(err));
        }
           msg.payload = output; 
        });
return msg;

```

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 09:20 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/4 "2025-03-07T09:20:32Z")

</div>

@gregorius

Sorry, missed that part. However, result is still the same. I cannot make it work.

I mentioned .ast() to showing that one method produces some result, while other returns empty object

---

<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:** [7 March 2025 09:24 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/5 "2025-03-07T09:24:00Z")

</div>

Did you supply the data that you wish the expression to use. `...evaluate(arr)` May need to be a property of an object

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 10:26 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/6 "2025-03-07T10:26:35Z")

</div>

No, it doesn't make a difference.

Interestingly, after your reply, I looked into the JSONata documentation to find information on providing arguments inside `evaluate()`.  
[[Embedding and Extending JSONata · JSONata](https://docs.jsonata.org/embedding-extending)]

There, they use `await` in front of the `evaluate()` expression.

I added `await`, and it worked. However, using `.evaluate(arr)` does not make a difference, meaning both of the following produce the same result. **But I don't know why?**

So, as of know we have two ways of getting our answer.

1. Method 1 -\> Using node-red util method that you mentioned above.

2. Method 2 -\> Using await infront of evaluate expression

```auto
let arr = ["He", "is", "a", "good", "boy"]

let temp = []

temp[0] = await jsonata(`$length("${arr[0]}")`).evaluate();
temp[1] = await jsonata(`$length("${arr[1]}")`).evaluate();
temp[2] = await jsonata(`$length("${arr[2]}")`).evaluate();
temp[3] = await jsonata(`$length("${arr[3]}")`).evaluate();
temp[4] = await jsonata(`$length("${arr[4]}")`).evaluate();

msg.payload = temp;

return msg;

```

---

<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:** [7 March 2025 10:34 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/7 "2025-03-07T10:34:57Z")

</div>

Or this may work, untested.

```auto
let arr = ["He", "is", "a", "good", "boy"];
msg.payload = await jsonata("$$.$length($)").evaluate(arr);

```

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 10:42 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/8 "2025-03-07T10:42:06Z")

</div>

Yes, its working as well.

Can i have an explanation of second line of your code?

---

<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:** [7 March 2025 10:44 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/9 "2025-03-07T10:44:53Z")

</div>

$$ - the base context of data supplied e.g. arr  
.length($) - map through the base context (which is an array) and apply $length to each index value e.g $ ( the values of the map loop context)

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 10:52 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/10 "2025-03-07T10:52:29Z")

</div>

$$ - base array  
.length($) - iterates over each element of base array returns length

Thanks for your support.

---

<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:** [7 March 2025 10:58 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/11 "2025-03-07T10:58:09Z")

</div>

> [@shubhamnodered](#):
>
> $$ - base array

Just clarify the base context is an arr as the supplied data arr is an array. if you supply an object then the base context would be an object, thus a slightly different expression would be required.

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [7 March 2025 11:08 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/12 "2025-03-07T11:08:52Z")

</div>

Yeah, I'm trying to work on it. Though i got answer using long method, short method is taking me some time. Let me give it some more tries

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [8 March 2025 08:14 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/13 "2025-03-08T08:14:11Z")

</div>

This is shortest method i came up with for Objects

```auto
const example = { "a": 4, "b": 4.4, "c": 4.8, "d": -4.4 };
msg.payload = await jsonata(`$each($$, function($v){$floor($v)})`).evaluate(example);
return msg;

```

Output i got **`[4,4,4,-5]`**

But i cannot able in this format **` { "a": 4, "b": 4, "c": 4, "d": -5 }`**. Can you give me some kind of hint? Though long method gave us the desired result.

I did tried creating a array of keys and array of values seperately and used zip function, but that will create array of array

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [8 March 2025 08:38 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/14 "2025-03-08T08:38:35Z")

</div>

This is how my short method look like:

```auto
const example = { "a": 4, "b": 4.4, "c": 4.8, "d": -4.4 };
const arr_of_obj = await jsonata(`$each($$, function($v, $k){{$k : $floor($v)}})`).evaluate(example);
msg.payload = await jsonata("$merge($$)").evaluate(arr_of_obj);
return msg;

```

Are you doing it in same manner?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 March 2025 08:51 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/15 "2025-03-08T08:51:20Z")

</div>

Why don't you just use javascript?

```auto
Object.keys(msg.payload).forEach(key => {
    msg.payload[key] = Math.floor(msg.payload[key])
});

```

---

<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:** [8 March 2025 08:53 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/16 "2025-03-08T08:53:37Z")

</div>

```auto
$merge(
    $each($$, function($v,$k){
        {$k: $floor($v)}
    })
)

```

or

```auto
(
$props := $keys($$);
    $$.*#$i.${$props[$i]:$floor($)}
)

```

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [8 March 2025 09:03 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/18 "2025-03-08T09:03:16Z")

</div>

@Colin.

My Goal was to get proficient on working with JSONATA expression. Though your approach is good if i plan to execute using JS.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 March 2025 09:03 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/19 "2025-03-08T09:03:56Z")

</div>

> [@shubhamnodered](#):
>
> My Goal was to get proficient on working with JSONATA expression.

OK, understood.

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [8 March 2025 09:06 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/20 "2025-03-08T09:06:13Z")

</div>

Ok, at last we got the desired for object using $each and $merge but at the cost of readability.

---

<div class="post-metadata">

**Author:** ![shubhamnodered](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@shubhamnodered](https://discourse.nodered.org/u/shubhamnodered)\
**Post date:** [8 March 2025 09:06 UTC](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834/21 "2025-03-08T09:06:39Z")

</div>

@gregorius , @E1cid , @Colin .

Thank you all.

[Next page](https://discourse.nodered.org/t/jsonata-evaluate-expression-inside-function-node-is-returning-empty-object-at-debug-window/95834.md?page=2)
