JSONata retrieve value

how to retrieve the value (true/false) by using JSONata, I tried it but still failed. I want to take true value for on and false value for off.

[
    {
        "id": "075b37ac52f089bf",
        "type": "inject",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"result\":{\"active_time\":1665121978,\"biz_type\":18,\"category\":\"oz\",\"create_time\":1665121978,\"icon\":\"smart/icon.jpg\",\"id\":\"eb893b4451760\",\"ip\":\"103.231.32.221\",\"lat\":\"-6.1340\",\"local_key\":\"ee31f6fe58f\",\"lon\":\"106.86\",\"model\":\"EU02A-6A\",\"name\":\"Smart Plug\",\"online\":true,\"owner_id\":\"15754282\",\"product_id\":\"ajrgey5eya\",\"product_name\":\"Plug \",\"status\":[{\"code\":\"switch_1\",\"value\":true},{\"code\":\"countdown_1\",\"value\":0},{\"code\":\"add_ele\",\"value\":0},{\"code\":\"cur_current\",\"value\":0},{\"code\":\"cur_power\",\"value\":0},{\"code\":\"cur_voltage\",\"value\":2279},{\"code\":\"relay_status\",\"value\":\"last\"},{\"code\":\"cycle_time\",\"value\":\"\"},{\"code\":\"random_time\",\"value\":\"\"}],\"sub\":false,\"time_zone\":\"+07:00\",\"uid\":\"az158583\",\"update_time\":1665122537,\"uuid\":\"2d8587\"},\"success\":true,\"t\":1665133060242,\"tid\":\"19138202461e11ed9a263efafdcacd21\"}",
        "payloadType": "str",
        "x": 470,
        "y": 1420,
        "wires": [
            [
                "de3c7d01bf1c9416"
            ]
        ]
    },
    {
        "id": "d55f36e17ef6eab5",
        "type": "switch",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "property": "payload.result.status[0].value",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "true",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "false",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 790,
        "y": 1480,
        "wires": [
            [
                "a3eb0b1492ca3831"
            ],
            [
                "49b5497428113cac"
            ]
        ]
    },
    {
        "id": "dfaf33c6f53e500f",
        "type": "debug",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1130,
        "y": 1480,
        "wires": []
    },
    {
        "id": "a3eb0b1492ca3831",
        "type": "change",
        "z": "4bf109a5.a97ad8",
        "name": "on",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "on",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 930,
        "y": 1460,
        "wires": [
            [
                "dfaf33c6f53e500f"
            ]
        ]
    },
    {
        "id": "49b5497428113cac",
        "type": "change",
        "z": "4bf109a5.a97ad8",
        "name": "off",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "off",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 930,
        "y": 1500,
        "wires": [
            [
                "dfaf33c6f53e500f"
            ]
        ]
    },
    {
        "id": "293954b0559e0b57",
        "type": "inject",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"result\":{\"active_time\":1665121978,\"biz_type\":18,\"category\":\"oz\",\"create_time\":1665121978,\"icon\":\"smart/icon.jpg\",\"id\":\"eb893b4451760\",\"ip\":\"103.231.32.221\",\"lat\":\"-6.1340\",\"local_key\":\"ee31f6fe58f\",\"lon\":\"106.86\",\"model\":\"EU02A-6A\",\"name\":\"Smart Plug\",\"online\":true,\"owner_id\":\"15754282\",\"product_id\":\"ajrgey5eya\",\"product_name\":\"Plug \",\"status\":[{\"code\":\"switch_1\",\"value\":false},{\"code\":\"countdown_1\",\"value\":0},{\"code\":\"add_ele\",\"value\":0},{\"code\":\"cur_current\",\"value\":0},{\"code\":\"cur_power\",\"value\":0},{\"code\":\"cur_voltage\",\"value\":2279},{\"code\":\"relay_status\",\"value\":\"last\"},{\"code\":\"cycle_time\",\"value\":\"\"},{\"code\":\"random_time\",\"value\":\"\"}],\"sub\":false,\"time_zone\":\"+07:00\",\"uid\":\"az158583\",\"update_time\":1665122537,\"uuid\":\"2d8587\"},\"success\":true,\"t\":1665133060242,\"tid\":\"19138202461e11ed9a263efafdcacd21\"}",
        "payloadType": "str",
        "x": 470,
        "y": 1520,
        "wires": [
            [
                "de3c7d01bf1c9416"
            ]
        ]
    },
    {
        "id": "de3c7d01bf1c9416",
        "type": "json",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 650,
        "y": 1480,
        "wires": [
            [
                "d55f36e17ef6eab5",
                "b96ed1f4f6cadf1e"
            ]
        ]
    },
    {
        "id": "b96ed1f4f6cadf1e",
        "type": "debug",
        "z": "4bf109a5.a97ad8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 930,
        "y": 1380,
        "wires": []
    }
]

I hope someone can help me.

You are testing in the switch if the property is equal to the string "true", but your object status[0] value is a boolean true
you need to test for is true or is false


No JSONata required.

If you want to do it with JSONata, because the position of code switch1 changes in the array.
Then you could do this.

[{"id":"075b37ac52f089bf","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"result\":{\"active_time\":1665121978,\"biz_type\":18,\"category\":\"oz\",\"create_time\":1665121978,\"icon\":\"smart/icon.jpg\",\"id\":\"eb893b4451760\",\"ip\":\"103.231.32.221\",\"lat\":\"-6.1340\",\"local_key\":\"ee31f6fe58f\",\"lon\":\"106.86\",\"model\":\"EU02A-6A\",\"name\":\"Smart Plug\",\"online\":true,\"owner_id\":\"15754282\",\"product_id\":\"ajrgey5eya\",\"product_name\":\"Plug \",\"status\":[{\"code\":\"switch_1\",\"value\":true},{\"code\":\"countdown_1\",\"value\":0},{\"code\":\"add_ele\",\"value\":0},{\"code\":\"cur_current\",\"value\":0},{\"code\":\"cur_power\",\"value\":0},{\"code\":\"cur_voltage\",\"value\":2279},{\"code\":\"relay_status\",\"value\":\"last\"},{\"code\":\"cycle_time\",\"value\":\"\"},{\"code\":\"random_time\",\"value\":\"\"}],\"sub\":false,\"time_zone\":\"+07:00\",\"uid\":\"az158583\",\"update_time\":1665122537,\"uuid\":\"2d8587\"},\"success\":true,\"t\":1665133060242,\"tid\":\"19138202461e11ed9a263efafdcacd21\"}","payloadType":"str","x":110,"y":680,"wires":[["de3c7d01bf1c9416"]]},{"id":"de3c7d01bf1c9416","type":"json","z":"452103ea51141731","name":"","property":"payload","action":"","pretty":false,"x":290,"y":740,"wires":[["b96ed1f4f6cadf1e","a3eb0b1492ca3831"]]},{"id":"293954b0559e0b57","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"result\":{\"active_time\":1665121978,\"biz_type\":18,\"category\":\"oz\",\"create_time\":1665121978,\"icon\":\"smart/icon.jpg\",\"id\":\"eb893b4451760\",\"ip\":\"103.231.32.221\",\"lat\":\"-6.1340\",\"local_key\":\"ee31f6fe58f\",\"lon\":\"106.86\",\"model\":\"EU02A-6A\",\"name\":\"Smart Plug\",\"online\":true,\"owner_id\":\"15754282\",\"product_id\":\"ajrgey5eya\",\"product_name\":\"Plug \",\"status\":[{\"code\":\"switch_1\",\"value\":false},{\"code\":\"countdown_1\",\"value\":0},{\"code\":\"add_ele\",\"value\":0},{\"code\":\"cur_current\",\"value\":0},{\"code\":\"cur_power\",\"value\":0},{\"code\":\"cur_voltage\",\"value\":2279},{\"code\":\"relay_status\",\"value\":\"last\"},{\"code\":\"cycle_time\",\"value\":\"\"},{\"code\":\"random_time\",\"value\":\"\"}],\"sub\":false,\"time_zone\":\"+07:00\",\"uid\":\"az158583\",\"update_time\":1665122537,\"uuid\":\"2d8587\"},\"success\":true,\"t\":1665133060242,\"tid\":\"19138202461e11ed9a263efafdcacd21\"}","payloadType":"str","x":110,"y":780,"wires":[["de3c7d01bf1c9416"]]},{"id":"b96ed1f4f6cadf1e","type":"debug","z":"452103ea51141731","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":570,"y":640,"wires":[]},{"id":"a3eb0b1492ca3831","type":"change","z":"452103ea51141731","name":"on/off","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.result.status[code = \"switch_1\"].value ? \"on\" : \"off\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":740,"wires":[["dfaf33c6f53e500f"]]},{"id":"dfaf33c6f53e500f","type":"debug","z":"452103ea51141731","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":610,"y":740,"wires":[]}]

working well..thanks a lot..

image

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