Broken Jsonata --> Broken Node-RED

Hi There!

Sorry if this has been asked before however I just had the following problem. Flow:

[{"id":"c0434d6638a5e339","type":"change","z":"d7f182b9ccdd6c48","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t    fubar: \"sadasd\",\t    msg: msg.payload\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":497,"y":534,"wires":[["34f8a07edb2fde5c"]]},{"id":"34f8a07edb2fde5c","type":"debug","z":"d7f182b9ccdd6c48","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":742,"y":610,"wires":[]},{"id":"4926c043fb635466","type":"http in","z":"d7f182b9ccdd6c48","name":"","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":361,"y":446,"wires":[["c0434d6638a5e339"]]}]

Screen Shot 2023-07-29 at 17.21.15

The set msg.payload node has jsonata definition which isn't in error if looking at the properties sidebar:

Screen Shot 2023-07-29 at 17.19.23

When I go to the test tab there is an error:

Screen Shot 2023-07-29 at 17.19.08

The problem is that I can deploy this broken Jsonata no problems. When I then make the web request to the http-in node, Node-RED crashes with:

rednode-mnmn_1  | 29 Jul 15:19:32 - [red] Uncaught Exception:
rednode-mnmn_1  | 29 Jul 15:19:32 - [error] TypeError: Converting circular structure to JSON
rednode-mnmn_1  |     --> starting at object with constructor 'Socket'
rednode-mnmn_1  |     |     property 'parser' -> object with constructor 'HTTPParser'
rednode-mnmn_1  |     --- property 'socket' closes the circle
rednode-mnmn_1  |     at JSON.stringify (<anonymous>)
rednode-mnmn_1  |     at /usr/src/node-red/node_modules/jsonata/jsonata.js:5474:29
rednode-mnmn_1  |     at String.replace (<anonymous>)
rednode-mnmn_1  |     at populateMessage (/usr/src/node-red/node_modules/jsonata/jsonata.js:5473:31)
rednode-mnmn_1  |     at catchHandler (/usr/src/node-red/node_modules/jsonata/jsonata.js:5558:25)
minemind_rednode-mnmn_1 exited with code 1

I tried doing the same with an inject node, but that didn't break Node-RED, it seems to only happen when an http-in is used.

I was wondering why Node-RED doesn't run a test on the Jsonata property on edit save? That would have shown me the error far earlier. (I spent about half an hour search for this mistake on another server because I didn't check the test tab :frowning:)

I'm using 3.0.2, perhaps this is already addressed in 3.1.x?


IS fubar and msg in the JSONata expression supposed to be a var name or a property name? If the latter it should be quoted.

The expression does not show an error and is not broken, as fubar and msg are taken for variables msg.msg and msg.fubar.

fubar should be "fubar" i.e. a string but I defined it as javascript object:

  fubar: "this is fine in javascript"

but as Jsonata it must be:

  "fubar": "this is the same defined in jsonata"

and that's the problem: mixing javascript and jsonata. I don't know, but keys can't be variables in jsonata, so fubar must be a string.

EDIT: the problem is that it was invalid jsonata that I could deploy - it shouldn't be deployable IMHO.

keys can be varianbles but the variable value must be a string.

Again the jsonata expression is not invalid, as msg.msg could be a string, same with fubar.

Ah, ok you're saying that if fubar was defined on the msg object, then it would be fine?

Yes if msg.fubar was a string the expression would be fine, and msg.msg.

Ah ok, thanks for that :slight_smile: that explains it :+1:.

Just tested and yep the problem was the use of msg as key, that is defined (obviously) and broke everything and hence the strange error message from Node-RED.

I didn't know that little fact about having keys as variables ...

Just noticed you used msg.payload, when you use msg, jsonata goes into compatability mode. At that point your msg is taken as the msg object. Best not to add msg. I prefer to add $$.payload, which helps me know i am reffering to a msg property. You can omit the $$, but under certain circumstances you will need to add $$ to get to base context msg.

Ok, it seems I have a lot to learn about jsonata :frowning:

My use case is very simple and it's funny how two quotes caused all the hassle ... 2 bytes and 40 cm away from the screen is the problem :wink:

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