Inject: inconsistent JSONata error message

Hi There,

I'm just playing around with a broken JSONata expression: $match($$.payload, /./,-1) - the error here is "Invalid JSONata expression: Argument 1 of function "match" does not match function signature"

That is what I get from the change & switch nodes, however with the inject node I get [object Object] as error message:

it should really be the same as from the change and switch nodes. I've done nothing special in the inject node:

Another an inconsistency that all errors are exceptions except when the JSONata is used as a property retrieval in a switch node:

that's responsible for the yellow warning message in the debug log in the screenshot.

This was all done with NR 4.0.5.

[{"id":"13856856d2486190","type":"inject","z":"b2fcc5806b2715c0","name":"","props":[{"p":"payload"},{"p":"matchresult","v":"$match($$.payload, /./,-1)","vt":"jsonata"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"111","payloadType":"str","x":468,"y":603,"wires":[["3c34ef77a739bce7","b81d8f30282eb38d"]]},{"id":"b644be4896201b62","type":"change","z":"b2fcc5806b2715c0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$match($$.payload, /./,-1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":719,"y":827,"wires":[[]]},{"id":"dcdfd47201e8d434","type":"inject","z":"b2fcc5806b2715c0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":492,"y":720,"wires":[["b644be4896201b62","9d10f985e3d921e1","9edc98ee837ebbde","4397b7eba0ebeefb"]]},{"id":"9d10f985e3d921e1","type":"switch","z":"b2fcc5806b2715c0","name":"","property":"$match($$.payload, /./,-1)","propertyType":"jsonata","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":750,"y":658,"wires":[[]]},{"id":"9edc98ee837ebbde","type":"switch","z":"b2fcc5806b2715c0","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"$match($$.payload, /./,-1)","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":810,"y":722,"wires":[[]]},{"id":"4397b7eba0ebeefb","type":"switch","z":"b2fcc5806b2715c0","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"$match($$.payload, /./,-1)","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":868,"y":782,"wires":[[]]}]
1 Like

Please do raise bugs when you find things that are clearly wrong… [object Object] is never going to be a valid error message :wink:

I would once confirmed --> perhaps I'm doing something wrong - so I'd prefer to post here, get feedback and then create issue/PR at github.

This one was something I found while doing something else, so this post is kind of a reminder for me to create a proper issue.

EDIT: GitHub issue #5270

1 Like

@gregorius What does $$.payload mean in jsonata? And what is your match expression doing? Im not fluent in jsonata

$$ locks the context to the root of the input no matter where you are inside the created object. So $$.payload will always be the msg.payload.

2 Likes

Yep, $$ is the msg object, used to be msg but then it was changed to $$.

It's broken! :wink:

$match($$.payload, /./,-1) --> -1 is the limit then humber of matches, minus one is an illegal value.

What $match(..) does is take a string ($$.payload i.e. the value of the payload), match the regexp (in this case /./ meaning match a single character).

So for example, if the payload was "abcde" and the limit was 2, match returns [{"match":"a","index":0,"groups":[]},{"match":"b","index":1,"groups":[]}]

It's well worth getting into JSONata because it saves a lot of JS code in functions --> jsonata.org is good resource although the JSONata editor in Node-RED is fantastic:

it even has a test tool

1 Like

JSONata is great when it is great. Just don't spend too long on any single problem. If it is taking you more than a few minutes to work out the formula, switch to a function node because it can be a very deep and mind bending rabbit hole. :smiley:

If it is going to take more than a few minutes to create, there's a good chance you won't remember how it works in 6 months time.

Anyway, that's the rule I work to.

1 Like

Then use AI.

Why bother with the annoying step of coding the JS yourself in the function node? Slight tongue in cheek here but the point is : what seems hard for you, is easy for me and vice versus. There will be folks who would use AI instead - I know you're not one of them, neither am I.

So in the same vain I could have stopped learning English because in the beginning it took me ages to understand, I could just have stuck to my mother tongue. Or instead, I make the effort and discover a completely new and interesting world (english humour is far better than any german "stuff" - they call it humour but I don't know).

I think giving up on JSONata is a big mistake. Sure it's a major pain to learn in the beginning, not disagreeing on that. But it can be a great tool in the long run, literally saves time over creating function nodes all over the place.

Also it has consistency, so that once you have seen the patterns, you can imagine how it would work in JSONata. It would be nice to be able to extend JSONata with other features but that would be truly confusing and even I would be reaching for AI :wink:

Well, that's the thing. I don't find coding JS at all as annoying has having to deal with overly complex JSONata. :smiley:

Yes, of course, that's why I said it was how I work, not necessarily right for everyone.

It is a risk/reward question. Learning your native language is a pain but necessary for life. That might be also true of other languages depending on your country and job.

Learning JavaScript is a skill that transcends client (mobile, laptop and desktop) and server compute tasks and so is well worth learning - if you are into that kind of thing.

JSONata, while fab for some data reshaping tasks, is relatively limited in use. Therefore not everyone will want to invest loads of time into it. For me personally, it is like XLST or Regex or SQL, a domain-specific language. Worth knowing but not necessarily worth spending loads of effort on. It also has a significant performance overhead I believe (not tested it recently so not sure how much, if any, that has changed), so might not always be the best tool for high throughputs?

I'm not diss'ing JSONata, simply expressing a view. I remember having similar feelings about XLST. At a time when I was processing XML documents like I now process JSON, XLST was both amazing and a pain in the butt at the same time. Amazing to be able to radically reshape an XML document with a few lines of code but brain-meltingly hard to formulate the code sometimes (usually actually).

Of course, you must remember that I was also an APL programmer in a dim and distant past. That language is written using math symbols and it was far from unusual to spend a week perfecting a single line of code that would completely reshape and compute a complex multi-dimensional array. But again, we used to call that a "write-only" language. Because coming back to code after 6 months, you might as well start again because working out how it did what it did was as hard as writing it in the first place. No AI to help in those days, just hard slog.

1 Like

Don't forget, a non-programmer looking at Javascript would think the same of JS: it's brain melting.

Just because we are coders does not mean that all programming languages are difficult or easy for everyone else.

I don't find JSONata nor XSLT "brain melting", what I find brain melting is Prolog or Erlang but it's fun to step into the thought world and discover how concise code can become done declarative.