# JSONata retrieve value

**URL:** <https://discourse.nodered.org/t/jsonata-retrieve-value/68671>\
**Category:** General\
**Created:** [7 October 2022 10:32 UTC](https://discourse.nodered.org/t/jsonata-retrieve-value/68671 "2022-10-07T10:32:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [7 October 2022 10:32 UTC](https://discourse.nodered.org/t/jsonata-retrieve-value/68671/1 "2022-10-07T10:32:11Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/954ad5e347afe6c18a31064e4e4eec35f00bea56.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 October 2022 11:40 UTC](https://discourse.nodered.org/t/jsonata-retrieve-value/68671/2 "2022-10-07T11:40:43Z")

</div>

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

 ![chrome_screenshot_1665142689552](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89b8a9394fec9a801654b040d616121c05542fbd.jpeg)  
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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![wiwan-wijoyo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wiwan-wijoyo/32/48198_2.png) [@wiwan-wijoyo](https://discourse.nodered.org/u/wiwan-wijoyo)\
**Post date:** [7 October 2022 13:48 UTC](https://discourse.nodered.org/t/jsonata-retrieve-value/68671/3 "2022-10-07T13:48:57Z")

</div>

working well..thanks a lot..

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1299425dbf5d8e237734494b3915ce159e17dbd.png)

---

<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:** [21 October 2022 13:49 UTC](https://discourse.nodered.org/t/jsonata-retrieve-value/68671/4 "2022-10-21T13:49:28Z")

</div>

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