JSONata falls back to compatibility mode when there's a string ' msg ' in it

JSONata falls back to compatibility mode when there's a string ' msg ' (msg with spaces on both sides) in it. Is this intentional? (Node-RED 3.1.3 on NodeJS 18 and 20)

This means that this JSONata expression doesn't give back the payload:

"foo msg -" & payload

while this one works as I'd expect:

"msg -" & payload

Demonstration flow:

[
    {
        "id": "f08d2f8c04cbd984",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "342e03da63c86385",
        "type": "inject",
        "z": "f08d2f8c04cbd984",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 200,
        "y": 80,
        "wires": [
            [
                "781b0f561050b4d2"
            ]
        ]
    },
    {
        "id": "781b0f561050b4d2",
        "type": "change",
        "z": "f08d2f8c04cbd984",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payloadstring",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 370,
        "y": 80,
        "wires": [
            [
                "6189491046e60713"
            ]
        ]
    },
    {
        "id": "5a7bbf556fb06a5c",
        "type": "debug",
        "z": "f08d2f8c04cbd984",
        "name": "debug msg object",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 200,
        "wires": []
    },
    {
        "id": "6189491046e60713",
        "type": "change",
        "z": "f08d2f8c04cbd984",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "JSONata-value1",
                "pt": "msg",
                "to": "\"foo msg - \" & payload",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 140,
        "wires": [
            [
                "ef3ae5a3f618a952"
            ]
        ]
    },
    {
        "id": "ef3ae5a3f618a952",
        "type": "change",
        "z": "f08d2f8c04cbd984",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "JSONata-value2",
                "pt": "msg",
                "to": "\"msg - \" & payload",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 200,
        "wires": [
            [
                "5a7bbf556fb06a5c"
            ]
        ]
    },
    {
        "id": "30dffede0d1ea381",
        "type": "comment",
        "z": "f08d2f8c04cbd984",
        "name": "the first JSONata expression accidentally falls back to compatibility mode ",
        "info": "",
        "x": 820,
        "y": 140,
        "wires": []
    }
]

I can file an issue on Github if needed.

I think this is a left-over from the initial JSONata integration. Back then, you had to access message properties using msg.myProp. This has been changed at some point were you had properties directly available in your expression context.

There's a check in place that restores the old behaviour (the compatibilty mode) when you try the msg. approach.
If think it's a regexp, which fails some conditions like in your case.

As a workaround, I'd simply rename the text "msg" into something different until the warning disappears. :see_no_evil:

I've stumbled over that myself sometimes. I wonder if that "compatibilty mode" could finally be removed in some major release. :sweat_smile:

For reference, here's the relevant line of code:

Hm, not sure that regexp is optimal, but I don't know enough about what it is intended to catch to propose an alternative. Maybe it should include /s (to catch any whitespace around msg) in both of the exclusion groups. But yes, it's easy enough to work around by not using the specific string.

Yes, I know. At least you get a warning that it's activated, easy enough to work around then.

My hope is, that eventually the compatibility mode gets deprecated or even removed in one of the next major releases. :sweat_smile:

I agree, I too would like to remove it!

Happy to hear thoughts on how we handle / approach removalal of legacy mode?

Please continue any discussion in the issue raised: JSONata falls back to compatibility mode when there’s a string with ’ msg ’ in it · Issue #4532 · node-red/node-red · GitHub

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