# Invalid JSONata expression: Unable to cast value to a number

**URL:** https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549
**Category:** General
**Created:** [25 October 2022 10:18 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549 "2022-10-25T10:18:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Giacomo23](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@Giacomo23](https://discourse.nodered.org/u/Giacomo23)
#### Post date: [25 October 2022 10:18 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549/1 "2022-10-25T10:18:52Z")

</div>

hello everyone, I have to convert values to string, only that when I try to do an edit change node with expresion "$ number (payload)" it gives me the following error "Invalid JSONata expression: Unable to cast value to a number: "5\n5\n5\n5\n"", then I added a function that eliminates "/ n" and managed to bypass the error I got, unfortunately, however, the result is not what I would have expected but they delete "/ n" all the values went in a single row thus doing a wrong average of the values subsequently, how can I solve this problem?

 ![5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/8609cd67487e28242b79b358dc9ba3cb2580640b.png)  
 ![6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98a0f7d0f559ddcc4d8a8fb6242aa9e4663039e6.png)  
 ![0](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/776481a3f3e42d4b6bb15161704fef04282b7914.png)  
 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/28f90d190a04e5fe58df2b99f2b87de91d9e3820.png)  
 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0b700d15ee651a7e7ca35ee9019eb7300f60586.png)  
 ![3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5d6d05b73d3f73577fadd36281fd54884f0aa23.png)  
 ![4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce07127874219df058fb0ee43cc210e6722b9323.png)

Thanks for all

---

<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: [25 October 2022 10:36 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549/2 "2022-10-25T10:36:59Z")

</div>

Your post is very confusing, are you saying you wish to average the four 5's, which would be 5. Please explain if not.  
Can we also see a debug after the file node.  
Also best to post code as text as no one can copy the text from an image.

To take the string

```auto
5
5
5
5

```

You can pass it through a change node and apply the JSONata expression

```auto
$average($split($trim($$.payload), /\s/).$number($))

```

Output would be 5  
or

```auto
$sum($split($trim($$.payload), /\s/).$number($)) / 6

```

output would be 3.3333˙

---

<div class="post-metadata">

### Author: ![Giacomo23](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@Giacomo23](https://discourse.nodered.org/u/Giacomo23)
#### Post date: [25 October 2022 11:47 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549/3 "2022-10-25T11:47:24Z")

</div>

Thanks for the reply and I apologize if I did not make myself understood, however the solution you have proposed is fine  
"$ sum ($ split ($ trim ($$. payload), /\s/).$number($)) / 6"  
I just wanted to know if it was possible to have an integer as a solution for example:  
if the result is this "3.3333333333333335" I want this "3" as output otherwise if I get "3.6666666" as output I would like to have this "4"  
thanks again

---

<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: [25 October 2022 12:15 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549/4 "2022-10-25T12:15:25Z")

</div>

If you look at [Numeric functions · JSONata](https://docs.jsonata.org/numeric-functions) You will see all functions you can apply to a number  
$round() round depending on decimal place  
$ceil() round up  
$floor() round down  
so

```auto
$round($sum($split($trim($$.payload), /\s/).$number($)) / 6)

```

---

<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: [8 November 2022 12:15 UTC](https://discourse.nodered.org/t/invalid-jsonata-expression-unable-to-cast-value-to-a-number/69549/5 "2022-11-08T12:15:25Z")

</div>

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