Using JSONata or not to use JSONata - that is the question

1,422 views
Skip to first unread message

Simon Walters

unread,
Mar 10, 2018, 4:05:06 AM3/10/18
to Node-RED
I'm having an internal fight with myself :)

I don't know javascript (as in I can google for stuff and copy/modify exisiting code but I wouldn't call myself a javascript programmer)

So, I found that we can use JSONata as an alternative means of manipulating information but its hard to work out how to do stuff as there is not a lot of info/examples around on T'internet

Then I'm spending more time trying to workout how to do it than I would have had I just written some javascript in a function node

And then I start thinking - why am I spending time trying to work out a 3rd method (on top of working out using standard nodes and then javascript in function nodes)!!!!!!!

Then it occurs to me - how many others are having the the same dilemma  ? 

Would it not be better overall to just not bother with it and just use javascript inside function nodes - one programming script to rule em all! :)

I know we can just ignore it and not use it but that's not the standard mindset of a programmer is it? :)

Its an itch and we want to scratch it! :)

Discuss :)

Colin Law

unread,
Mar 10, 2018, 4:14:27 AM3/10/18
to node...@googlegroups.com
If you want to learn one thing at a time learn javascript so you can
write functions. Anything you can do in JSONata can be done in a
function, but not the other way round. Then when you are comfortable
with javascript you play with JSONata. I have to admit that I only
use JSONata for fairly simple stuff. I think you have to have a
particular sort of brain to cope with the more subtle uses of JSONata
and I am not sure that I do.

Colin

On 10 March 2018 at 09:05, 'Simon Walters' via Node-RED
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/7909804c-c5da-4c35-94c8-0f156415f3b1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Simon Walters

unread,
Mar 10, 2018, 4:22:15 AM3/10/18
to Node-RED
 "have to admit that I only 
use JSONata for fairly simple stuff. I think you have to have a 
particular sort of brain to cope with the more subtle uses of JSONata 
and I am not sure that I do. "

Thats my problem - its usually easier/takes less time to do simple stuff in JSONata ....

and then I want to do more .....
....and more.....

and a whole 24 hours goes by :)

Getting my mind around the NodeRED programming paradigm is hard enough without then trying to do the same with JSONata - then giving JSONata and moving to function mode!! :)

Dave C-J

unread,
Mar 10, 2018, 4:46:04 AM3/10/18
to node...@googlegroups.com
Well - Node-RED started as a way to save us from having to keep re-writing the boring bits of node.js (javascript) applications eg serial port drivers, http requests, mqtt publishers, etc... with the function node there to allow all the extra glue to be added. JSONata was added later as it's great for manipulating JSON messages which is what we pass around. If you had to learn one I'd say Javascript, as then as well as functions, you can also start writing your own nodes, and/or even extend the core as it's all Javascript.

Garry Hayne

unread,
Mar 10, 2018, 5:06:14 AM3/10/18
to Node-RED
I have gone down that road and have now fully embraced JSONata, it's been a very rewarding experience. 
It's not that I am doing very exotic stuff, but I rarely use function nodes nowadays.

Garry 

Garry Hayne

unread,
Mar 10, 2018, 5:10:05 AM3/10/18
to Node-RED

Dean Cording

unread,
Mar 10, 2018, 6:31:02 AM3/10/18
to Node-RED

Thats my problem - its usually easier/takes less time to do simple stuff in JSONata ....

and then I want to do more .....
....and more.....

And that's the problem with JSONata, it's a bit too powerful for a filter and the temptation is always there to push the boundaries.  


I think you have to have a particular sort of brain to cope with the more subtle uses of JSONata 
and I am not sure that I do. 

Personally, I can see the use of JSONata resulting in a lot of unmaintainable code - it's not easy to understand what it is doing when you are writing the code, let alone 12 months later when you need to change it.

As a rule of thumb, I use the following principles as a guide:

1.  Do as much using nodes and flows as possible - that is the reason for using Node Red in the first place
2.  If it involves object property manipulation, then JSONata is the tool of choice
3.  If there is logic involved, then Javascript functions are the best solution (and if I need to write a Javascript in a function node,then I might as turn it into a proper node so that I can apply rule 1).

  
Dean

Dave C-J

unread,
Mar 10, 2018, 6:50:36 AM3/10/18
to node...@googlegroups.com
I agree with that decision tree

Bart Butenaers

unread,
Mar 10, 2018, 7:24:49 AM3/10/18
to Node-RED
Hey Dean,

Looks like we are using the same decision tree for our hobby projects...
But mine has an extra step at the end:

4. If the function node code can be reused, then try to make a custom node (and publish it on npm).

And yeah, I know that not all users have enough technical skills to accomplish this.
But for those that are already able to write a function node, it is only a (rather) small step to a custom node...

Bart

Simon Walters

unread,
Mar 10, 2018, 7:26:50 AM3/10/18
to Node-RED
Its knowing when to switch from Plan2 to Plan3 :)
Maybe we should just cripple JSONata so it can't do anything more than the basics SonofJSONata :() xkcd.com/927 obligatory reference at his point :)

As a current practical issue - should I try to use JSONata to go over this array of values (a sensor update msg from Scratch - 1st 4 bytes are length of msg and can be ignored as whole message comes in at once except for very complex/lots of data) and convert the char values back to a string in JSONata ?

[0,0,0,25,115,101,110,115,111,114,45,117,112,100,97,116,101,32,34,99,111,117,110,116,34,32,51,48,32]

Dean Cording

unread,
Mar 10, 2018, 7:46:22 AM3/10/18
to Node-RED


On Saturday, 10 March 2018 22:26:50 UTC+10, Simon Walters wrote:
Its knowing when to switch from Plan2 to Plan3 :)
Maybe we should just cripple JSONata so it can't do anything more than the basics SonofJSONata :() xkcd.com/927 obligatory reference at his point :)

As a current practical issue - should I try to use JSONata to go over this array of values (a sensor update msg from Scratch - 1st 4 bytes are length of msg and can be ignored as whole message comes in at once except for very complex/lots of data) and convert the char values back to a string in JSONata ?

[0,0,0,25,115,101,110,115,111,114,45,117,112,100,97,116,101,32,34,99,111,117,110,116,34,32,51,48,32]



Whilst I reckon JSONata could actually do it, a Javascript function node would be much simpler and comprehendible.


var string = '';
for (x=4; x < msg.payload.length; x++) {
 
string += String.fromCharCode(msg.payload[x]);
}

msg
.payload = string;

node
.send(msg);

 

Simon Walters

unread,
Mar 10, 2018, 8:03:39 AM3/10/18
to Node-RED
Yep - I tweeted out for help and got given that code as the answer as well :)

But could it be done in JSONata with something like a map function?

Or should I have just thought - it needs a loop - forget JSONata?

Nick O'Leary

unread,
Mar 10, 2018, 8:17:59 AM3/10/18
to node...@googlegroups.com
I don't believe there is an equivalent to String.fromCharCode available in jsonata, so stick with JavaScript in this instance.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
Mar 10, 2018, 8:44:20 AM3/10/18
to Node-RED
I tend to use JSONata for all my msg payload manipulations (objects/arrays in -> objects/arrays out). This case does not fall into that category for me, since the output is a string, and as Nick mentions, there are not functions that manipulate characters -- because that is not the language's primary purpose. If you have ever worked with XML/XSL/XPath, then JSON/JSONata would be the equivalent for JS objects...

As for Javascript, I don't think you can really unlock the potential of node-red without at least a basic understanding of JS objects and the nodejs package manager (npm). Of course, you can go too far down that path, and write your whole flow inside function nodes using javascript -- but that's just tedious and not maintainable either. Even when I can use function nodes for some logic, I try to reuse single-purpose core nodes, to make my flows more readable.

BTW, when I see your array of data, I don't think of "looping" -- I think of filling a buffer:

msg.payload = Buffer.from(msg.payload.slice(4)).toString();
return msg;

--
Steve

Simon Walters

unread,
Mar 10, 2018, 9:18:33 AM3/10/18
to Node-RED
Buffer,array, string JSON object - no wonder I get confused :)
So what's the difference between a Buffer and a String in javascript?

steve rickus

unread,
Mar 11, 2018, 2:08:13 PM3/11/18
to Node-RED
Buffers are a way of handling binary data -- essentially backed by an array of integers. But when taken 2/4/8 bytes at a time, or interpreted as little/bit endian, those integers can represent all kinds of data formats. In your case, the binary "data" you received was just a string of characters. You can check out the Buffer Module built-in to nodejs (as well as Objects, Strings and Arrays) on the W3Schools site...

Jan Van den Audenaerde

unread,
Mar 12, 2018, 5:33:07 AM3/12/18
to Node-RED
I only want to add that node-red made it really easy to test your JSONata expression.  
You can easily copy the json message from your debug window into the JSONata expression test editor and immediately check if your expression is correct or not.
That is something you can not do that easily within javascript.

So I am definitely going to use this feature more (just learned about it this weekend).


Reply all
Reply to author
Forward
0 new messages