# JSONata vs JavaScript

**URL:** <https://discourse.nodered.org/t/jsonata-vs-javascript/61595>\
**Category:** General\
**Created:** [21 April 2022 12:20 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595 "2022-04-21T12:20:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudleyjosh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudleyjosh/32/105799_2.png) [@dudleyjosh](https://discourse.nodered.org/u/dudleyjosh)\
**Post date:** [21 April 2022 12:20 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/1 "2022-04-21T12:20:43Z")

</div>

Is there a good reason why the behavior of JSONata `$number(value)` is different than Javascript `Number(value)`?

e.g. `$number(".5")` will fail to cast as a number where as `Number(".5")` will cast to the numeric value of 0.5 as expected.

I continually find myself using Function nodes (when I don't really want to) because I run into issues like the above when using a Change node and JSONata.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 12:40 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/2 "2022-04-21T12:40:01Z")

</div>

You might need to ask that question on the JSONata site rather than here?

The other question I'd ask is why you prefer JSONata over JavaScript? JS is a lot more efficient in general and really, the only benefit of JSONata is when it can do object reshaping easily for you that would be complex in JavaScript.

---

<div class="post-metadata">

**Author:** ![dudleyjosh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudleyjosh/32/105799_2.png) [@dudleyjosh](https://discourse.nodered.org/u/dudleyjosh)\
**Post date:** [21 April 2022 13:32 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/3 "2022-04-21T13:32:18Z")

</div>

> [@TotallyInformation](#):
>
> The other question I'd ask is why you prefer JSONata over JavaScript?

It's not that I prefer JSONata over JS... it's just the paradigm of Node-RED being a 'low code' flow based, programming tool I was trying to minimize my usage of the Function node and rely on various functional type nodes (such as the Change node)... but I continually run into situations like this where I need to "sanitize" the incoming data and I end up reverting to Function nodes with bits of JS.

I just thought perhaps I was missing something or didn't understand how to use some of the built-in nodes effectively?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [21 April 2022 13:42 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/4 "2022-04-21T13:42:06Z")

</div>

JSONata is pretty strict. TBH ".5" is not something I would enter in any programming language, as it's easy not to see the decimal point when debugging. Just my opinion.

```auto
$number("0.5")	

```

works.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2022 13:51 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/5 "2022-04-21T13:51:09Z")

</div>

> [@dudleyjosh](#):
>
> I just thought perhaps I was missing something or didn't understand how to use some of the built-in nodes effectively?

😉 nope.

I think you will find that most people who know JavaScript but who use Node-RED use function nodes a lot because we can create a small nugget of code that itself does a lot in a few lines. But we use Node-RED because it can do a LOT with no code and it gives us an operating environment that is consistent and robust.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 April 2022 13:51 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/6 "2022-04-21T13:51:44Z")

</div>

JSONata is designed to work with strict JSON valid types. `".5"` is not a valid representation of a number in JSON, so JSONata doesn't handle it.

A case could certainly be made for it to be slightly more relaxed... but that's something to take up with the JSONata project.

---

<div class="post-metadata">

**Author:** ![dudleyjosh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudleyjosh/32/105799_2.png) [@dudleyjosh](https://discourse.nodered.org/u/dudleyjosh)\
**Post date:** [21 April 2022 15:41 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/7 "2022-04-21T15:41:18Z")

</div>

Thanks for the explanation Nick.

---

<div class="post-metadata">

**Author:** ![dudleyjosh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dudleyjosh/32/105799_2.png) [@dudleyjosh](https://discourse.nodered.org/u/dudleyjosh)\
**Post date:** [21 April 2022 15:46 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/8 "2022-04-21T15:46:02Z")

</div>

> [@ghayne](#):
>
> JSONata is pretty strict. TBH ".5" is not something I would enter in any programming language, as it's easy not to see the decimal point when debugging. Just my opinion.
> 
> ```auto
> $number("0.5")	
> 
> ```
> 
> works.

I don't disagree... but in this case I'm dealing with data being pulled from a database I have no control over. And when you are dealing with multi-purpose text fields, "numbers" can be stored any which way the user inputs them... thus the need to "sanitize" the data as it is pulled in.

---

<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:** [20 June 2022 15:46 UTC](https://discourse.nodered.org/t/jsonata-vs-javascript/61595/9 "2022-06-20T15:46:07Z")

</div>

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