JSONata string expression fails in flow but works in expression editor

Hello,

I'm thoroughly embarrassed here. I have a function that outputs an object. JSONata should extract some text but it fails. In fact, any JSONata string function seems to fail if it uses the output from my function directly, even though the expression editor test window doesn't report an error. Also, I've copied the output from my function into an inject node and the JSONata expression works.

I'm sorry about the long explanation.

Here's how the flow looks (code below) -

'call suncalc' contains -

msg.payload = suncalc.getTimes(new Date(), 50, 9);
return msg;

'jsonata test' contains -

$split(payload.sunriseEnd,"T")

(any string function seems to fail but, as a start, I'd be happy to understand how to call split).

If I click 'inject ts 1', JSONata complains -

The debug pane has the output of my function. If I copy and paste 'dbg suncalc out' from the debug window to the JSONata Expression editor, the same expression works fine, i.e. I get a result.

Because I've now lost my mind, I've pasted the payload from the debug window into function 'set same payload'. Clicking 'inject ts 2' results in the JSONata expression working.

Obviously I've done something silly but I can't see it. It seems the JSONata expression has looked at the same payload three times but worked only twice.

Here's the whole flow (I don't know why it doesn't appear on one line, and the forum button 'Preformatted Text' doesn't appear for me) -

[
    {
        "id": "088d1ec8c18917ac",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "b83e4c5efa9a9c89",
        "type": "inject",
        "z": "088d1ec8c18917ac",
        "name": "inject ts 1",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 100,
        "y": 40,
        "wires": [
            [
                "76b962a0b583bd6b"
            ]
        ]
    },
    {
        "id": "76b962a0b583bd6b",
        "type": "function",
        "z": "088d1ec8c18917ac",
        "name": "call suncalc",
        "func": "msg.payload = suncalc.getTimes(new Date(), 50, 9);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "suncalc",
                "module": "suncalc"
            }
        ],
        "x": 270,
        "y": 40,
        "wires": [
            [
                "7e4d958cb8ddabba",
                "d6153832242b2423"
            ]
        ]
    },
    {
        "id": "7e4d958cb8ddabba",
        "type": "debug",
        "z": "088d1ec8c18917ac",
        "name": "dbg suncalc out",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 460,
        "y": 40,
        "wires": []
    },
    {
        "id": "d6153832242b2423",
        "type": "change",
        "z": "088d1ec8c18917ac",
        "name": "jsonata test",
        "rules": [
            {
                "t": "set",
                "p": "why_do_you_fail",
                "pt": "msg",
                "to": "$split(payload.sunriseEnd,\"T\")\t ",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 100,
        "wires": [
            [
                "df2379a8b9a50369"
            ]
        ]
    },
    {
        "id": "72b4a4446469a442",
        "type": "function",
        "z": "088d1ec8c18917ac",
        "name": "set same payload",
        "func": "msg.payload = {\n    \"solarNoon\": \"2023-03-18T11:33:32.244Z\",\n    \"nadir\": \"2023-03-17T23:33:32.244Z\",\n    \"sunrise\": \"2023-03-18T05:33:38.199Z\",\n    \"sunset\": \"2023-03-18T17:33:26.289Z\",\n    \"sunriseEnd\": \"2023-03-18T05:36:57.242Z\",\n    \"sunsetStart\": \"2023-03-18T17:30:07.246Z\",\n    \"dawn\": \"2023-03-18T05:01:26.409Z\",\n    \"dusk\": \"2023-03-18T18:05:38.079Z\",\n    \"nauticalDawn\": \"2023-03-18T04:23:35.800Z\",\n    \"nauticalDusk\": \"2023-03-18T18:43:28.688Z\",\n    \"nightEnd\": \"2023-03-18T03:44:33.689Z\",\n    \"night\": \"2023-03-18T19:22:30.799Z\",\n    \"goldenHourEnd\": \"2023-03-18T06:16:20.745Z\",\n    \"goldenHour\": \"2023-03-18T16:50:43.743Z\"\n};\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [
            {
                "var": "suncalc",
                "module": "suncalc"
            }
        ],
        "x": 290,
        "y": 160,
        "wires": [
            [
                "ab75471910255169",
                "d6153832242b2423"
            ]
        ]
    },
    {
        "id": "ab75471910255169",
        "type": "debug",
        "z": "088d1ec8c18917ac",
        "name": "dbg suncalc out 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 160,
        "wires": []
    },
    {
        "id": "d93adbdcdecf9f83",
        "type": "inject",
        "z": "088d1ec8c18917ac",
        "name": "inject ts 2",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 100,
        "y": 160,
        "wires": [
            [
                "72b4a4446469a442"
            ]
        ]
    },
    {
        "id": "df2379a8b9a50369",
        "type": "debug",
        "z": "088d1ec8c18917ac",
        "name": "dbg jsonata out",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 640,
        "y": 100,
        "wires": []
    }
]

The suncalc function is returning time Objects you will need to convert to strings $type($$.payload.sunriseEnd) returns object

$split($string($$.payload.sunriseEnd),"T")	 

Hello,

Aargh, thanks!

The values are dates but I didn't think about this because the JSONata Expression Editor treats the values, because I was pasting in, as strings. Oops.

Cheers.

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