Invalid JSONata expression: Unable to cast value to a number

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?


6
0

2
3

Thanks for all

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

5
5
5
5

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

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

Output would be 5
or

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

output would be 3.3333Ë™

1 Like

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

If you look at Numeric functions · JSONata You will see all functions you can apply to a number
$round() round depending on decimal place
$ceil() round up
$floor() round down
so

$round($sum($split($trim($$.payload), /\s/).$number($)) / 6)
1 Like

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