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

**URL:** <https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692>\
**Category:** Core Development\
**Tags:** jsonata\
**Created:** [18 January 2024 16:31 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692 "2024-01-18T16:31:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RvG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rvg/32/96613_2.png) [@RvG](https://discourse.nodered.org/u/RvG)\
**Post date:** [18 January 2024 16:31 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/1 "2024-01-18T16:31:19Z")

</div>

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:

```auto
"foo msg -" & payload

```

while this one works as I'd expect:

```auto
"msg -" & payload

```

Demonstration flow:

```auto
[
    {
        "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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [18 January 2024 17:32 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/2 "2024-01-18T17:32:58Z")

</div>

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. 🙈

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

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

> <https://github.com/node-red/node-red/blob/f93654f68003614ef697d30e7b002d57b0a5961d/packages/node_modules/%40node-red/util/lib/util.js#L723>

---

<div class="post-metadata">

**Author:** ![RvG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rvg/32/96613_2.png) [@RvG](https://discourse.nodered.org/u/RvG)\
**Post date:** [19 January 2024 08:58 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/3 "2024-01-19T08:58:19Z")

</div>

> [@kuema](#):
>
> ```auto
> expr._legacyMode = /(^|[^a-zA-Z0-9_'".])msg([^a-zA-Z0-9_'"]|$)/.test(value);
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [19 January 2024 09:12 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/4 "2024-01-19T09:12:53Z")

</div>

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. 😅

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 January 2024 09:48 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/5 "2024-01-19T09:48:03Z")

</div>

> [@kuema](#):
>
> My hope is, that eventually the compatibility mode gets deprecated or even removed in one of the next major releases

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](https://github.com/node-red/node-red/issues/4532)

---

<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:** [19 March 2024 09:48 UTC](https://discourse.nodered.org/t/jsonata-falls-back-to-compatibility-mode-when-theres-a-string-msg-in-it/84692/6 "2024-03-19T09:48:58Z")

</div>

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