# JSONata, how to access json keys?

**URL:** <https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065>\
**Category:** General\
**Created:** [27 December 2023 17:49 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065 "2023-12-27T17:49:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [27 December 2023 17:49 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/1 "2023-12-27T17:49:30Z")

</div>

I am receiving this json from a Shelly H&T Plus

```auto

{
  "src": "shellyplusht-e465b85f5e54",
  "dst": "shelly/events",
  "method": "NotifyFullStatus",
  "params": {
    "ts": 1703698870.74,
    "ble": {},
    "cloud": {
      "connected": false
    },
    "devicepower:0": {
      "id": 0,
      "battery": {
        "V": 5.21,
        "percent": 60
      },
      "external": {
        "present": true
      }
    },
    "ht_ui": {},
    "humidity:0": {
      "id": 0,
      "rh": 57.6
    },
    "mqtt": {
      "connected": true
    },
    "sys": {
      "mac": "E465B85F5E54",
      "restart_required": false,
      "time": "18:41",
      "unixtime": 1703698870,
      "uptime": 2,
      "ram_size": 246304,
      "ram_free": 153748,
      "fs_size": 458752,
      "fs_free": 172032,
      "cfg_rev": 24,
      "kvs_rev": 0,
      "webhook_rev": 0,
      "available_updates": {},
      "wakeup_reason": {
        "boot": "deepsleep_wake",
        "cause": "status_update"
      },
      "wakeup_period": 600,
      "reset_reason": 8
    },
    "temperature:0": {
      "id": 0,
      "tC": 22.7,
      "tF": 72.8
    },
    "wifi": {
      "sta_ip": "192.168.178.75",
      "status": "got ip",
      "ssid": "FRITZ!Box 7530 KI",
      "rssi": -49
    },
    "ws": {
      "connected": false
    }
  }
}

```

I am trying to use a change node with JSONata to preparare data for a chart node [dashboard-2 palette]

> Ignore for a moment that the following code it is _not_ retrieving a _value_ for 'y', but an _object_. This question is about a JSONata syntax problem, i suppose. I only want to understand how to access keys in JSONata

I am using change node to prepare this payload

```auto

[
   {
       "y" : msg.payload.params["devicepower:0"],
       "series" : "Volts"
    },
   {
       "y" : msg.payload.params["devicepower:0"]["battery"],
       "series" : "Percentuale"
    }
]

```

When I debug the output `msg` I see that both the 'y' are `msg.payload.params`

I am not able i think to access `"devicepower:0"` in JSONata. what am I doing wrong?

The dump of final msg, actually is

```auto
[
  {
    "y": {
      "ts": 1.95,
      "ble": {},
      "cloud": {
        "connected": false
      },
      "devicepower:0": {
        "id": 0,
        "battery": {
          "V": 5.22,
          "percent": 60
        },
        "external": {
          "present": true
        }
      },
      "ht_ui": {},
      "humidity:0": {
        "id": 0,
        "rh": 57.6
      },
      "mqtt": {
        "connected": true
      },
      "sys": {
        "mac": "E465B85F5E54",
        "restart_required": false,
        "time": null,
        "unixtime": null,
        "uptime": 1,
        "ram_size": 246384,
        "ram_free": 155716,
        "fs_size": 458752,
        "fs_free": 172032,
        "cfg_rev": 24,
        "kvs_rev": 0,
        "webhook_rev": 0,
        "available_updates": {},
        "wakeup_reason": {
          "boot": "deepsleep_wake",
          "cause": "status_update"
        },
        "wakeup_period": 600,
        "reset_reason": 8
      },
      "temperature:0": {
        "id": 0,
        "tC": 22.6,
        "tF": 72.6
      },
      "wifi": {
        "sta_ip": "192.168.178.75",
        "status": "got ip",
        "ssid": "FRITZ!Box 7530 KI",
        "rssi": -49
      },
      "ws": {
        "connected": false
      }
    },
    "series": "Volts"
  },
  {
    "y": {
      "ts": 1.95,
      "ble": {},
      "cloud": {
        "connected": false
      },
      "devicepower:0": {
        "id": 0,
        "battery": {
          "V": 5.22,
          "percent": 60
        },
        "external": {
          "present": true
        }
      },
      "ht_ui": {},
      "humidity:0": {
        "id": 0,
        "rh": 57.6
      },
      "mqtt": {
        "connected": true
      },
      "sys": {
        "mac": "E465B85F5E54",
        "restart_required": false,
        "time": null,
        "unixtime": null,
        "uptime": 1,
        "ram_size": 246384,
        "ram_free": 155716,
        "fs_size": 458752,
        "fs_free": 172032,
        "cfg_rev": 24,
        "kvs_rev": 0,
        "webhook_rev": 0,
        "available_updates": {},
        "wakeup_reason": {
          "boot": "deepsleep_wake",
          "cause": "status_update"
        },
        "wakeup_period": 600,
        "reset_reason": 8
      },
      "temperature:0": {
        "id": 0,
        "tC": 22.6,
        "tF": 72.6
      },
      "wifi": {
        "sta_ip": "192.168.178.75",
        "status": "got ip",
        "ssid": "FRITZ!Box 7530 KI",
        "rssi": -49
      },
      "ws": {
        "connected": false
      }
    },
    "series": "Percentuale"
  }
]

```

As you can see, both the syntax are extracing only down to msg.params, and ignoring all square brackets. Why ?

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [27 December 2023 18:04 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/2 "2023-12-27T18:04:12Z")

</div>

I solved doing the mutation in 2 steps 🙂 but I'm interested in the main question anyway

```auto
[{"id":"1bd7a6baa1b4345f","type":"change","z":"dc5ba8fbe263b333","name":"Set message payload to battery","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.params.devicepower:0.battery","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"[\t {\t \"y\" : msg.payload.V,\t \"series\" : \"Volts\"\t },\t {\t \"y\" : msg.payload.percent,\t \"series\" : \"Percentuale\"\t }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":960,"wires":[["10e8392c9eb3c032","f9d030d4f05bdda5","d3779369a9129fb5"]]},{"id":"10e8392c9eb3c032","type":"debug","z":"dc5ba8fbe263b333","name":"battery","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":960,"wires":[]},{"id":"f9d030d4f05bdda5","type":"ui-chart","z":"dc5ba8fbe263b333","group":"0f768d0859c8489c","name":"","label":"Batteria","order":9007199254740991,"chartType":"line","category":"series","categoryType":"str","xAxisProperty":"","xAxisPropertyType":"msg","xAxisType":"time","yAxisProperty":"","ymin":"","ymax":"","showLegend":true,"removeOlder":1,"removeOlderUnit":"86400","removeOlderPoints":"100","colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"width":"3","height":"4","className":"","x":1020,"y":1020,"wires":[[]]},{"id":"d3779369a9129fb5","type":"ui-text","z":"dc5ba8fbe263b333","group":"489074d388044b1b","order":0,"width":0,"height":0,"name":"Payload batteria","label":"Payload batteria","format":"{{msg.payload}}","layout":"col-center","style":true,"font":"","fontSize":"20","color":"#717171","className":"","x":1040,"y":1060,"wires":[]},{"id":"650abb5334f9edbb","type":"inject","z":"dc5ba8fbe263b333","name":"Test json injection","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"src\":\"shellyplusht-e465b85f5e54\",\"dst\":\"shelly/events\",\"method\":\"NotifyFullStatus\",\"params\":{\"ts\":1703698870.74,\"ble\":{},\"cloud\":{\"connected\":false},\"devicepower:0\":{\"id\":0,\"battery\":{\"V\":5.21,\"percent\":60},\"external\":{\"present\":true}},\"ht_ui\":{},\"humidity:0\":{\"id\":0,\"rh\":57.6},\"mqtt\":{\"connected\":true},\"sys\":{\"mac\":\"E465B85F5E54\",\"restart_required\":false,\"time\":\"18:41\",\"unixtime\":1703698870,\"uptime\":2,\"ram_size\":246304,\"ram_free\":153748,\"fs_size\":458752,\"fs_free\":172032,\"cfg_rev\":24,\"kvs_rev\":0,\"webhook_rev\":0,\"available_updates\":{},\"wakeup_reason\":{\"boot\":\"deepsleep_wake\",\"cause\":\"status_update\"},\"wakeup_period\":600,\"reset_reason\":8},\"temperature:0\":{\"id\":0,\"tC\":22.7,\"tF\":72.8},\"wifi\":{\"sta_ip\":\"192.168.178.75\",\"status\":\"got ip\",\"ssid\":\"FRITZ!Box 7530 KI\",\"rssi\":-49},\"ws\":{\"connected\":false}}}","payloadType":"json","x":450,"y":960,"wires":[["1bd7a6baa1b4345f"]]},{"id":"0f768d0859c8489c","type":"ui-group","name":"Test vari","page":"908029e5742fdefd","width":"4","height":"3","order":-1,"showTitle":true,"className":""},{"id":"489074d388044b1b","type":"ui-group","name":"test vari 2","page":"908029e5742fdefd","width":"4","height":"3","order":-1,"showTitle":true,"className":""},{"id":"908029e5742fdefd","type":"ui-page","name":"Test","ui":"0a0c83096de9bc95","path":"/test","icon":"test-tube","layout":"grid","theme":"b9ee14e4f62015c3","order":-1,"className":""},{"id":"0a0c83096de9bc95","type":"ui-base","name":"Dashboard","path":"/dashboard","includeClientData":true,"acceptsClientConfig":["ui-notification","ui-control"]},{"id":"b9ee14e4f62015c3","type":"ui-theme","name":"tema","colors":{"surface":"#ffffff","primary":"#0094ce","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"}}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 December 2023 19:28 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/3 "2023-12-27T19:28:06Z")

</div>

Unless I'm missing something, the two data you are extracting seem to be `msg.payload.params["devicepower:0"].battery.V` and  
`msg.payload.params["devicepower:0"].battery.percent`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4aaa8d7105e3c32a6e2e8548fa716a972c9129a.png)

What's the problem?

---

<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:** [27 December 2023 20:02 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/4 "2023-12-27T20:02:58Z")

</div>

Hi

First please stop using msg in JSONata expressions, it sets JSONata into compatibility mode which is deprecated

from compatibility info

> The current expression appears to still reference `msg` so will be evaluated in compatibility mode. Please update the expression to not use `msg` as this mode will be removed in the future.

> When JSONata support was first added to Node-RED, it required the expression to reference the `msg` object. For example `msg.payload` would be used to access the payload.

> That is no longer necessary as the expression will be evaluated against the message directly. To access the payload, the expression should be just `payload`.

The issue you have in your expression is that the property name has chars that are non standard, so they need to be quoted. You can only use square bracket notation in arrays not objects.

So your expression would be something like this

```auto
[
   {
       "y" : $$.payload.params."devicepower:0".battery.V,
       "series" : "Volts"
    },
   {
       "y" : $$.payload.params."devicepower:0".battery.percent,
       "series" : "Percentuale"
    }
]

```

[edit]

But you could do it without any JSONata or Javascript  
e.g.

```auto
[{"id":"650abb5334f9edbb","type":"inject","z":"d1395164b4eec73e","name":"Test json injection","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"src\":\"shellyplusht-e465b85f5e54\",\"dst\":\"shelly/events\",\"method\":\"NotifyFullStatus\",\"params\":{\"ts\":1703698870.74,\"ble\":{},\"cloud\":{\"connected\":false},\"devicepower:0\":{\"id\":0,\"battery\":{\"V\":5.21,\"percent\":60},\"external\":{\"present\":true}},\"ht_ui\":{},\"humidity:0\":{\"id\":0,\"rh\":57.6},\"mqtt\":{\"connected\":true},\"sys\":{\"mac\":\"E465B85F5E54\",\"restart_required\":false,\"time\":\"18:41\",\"unixtime\":1703698870,\"uptime\":2,\"ram_size\":246304,\"ram_free\":153748,\"fs_size\":458752,\"fs_free\":172032,\"cfg_rev\":24,\"kvs_rev\":0,\"webhook_rev\":0,\"available_updates\":{},\"wakeup_reason\":{\"boot\":\"deepsleep_wake\",\"cause\":\"status_update\"},\"wakeup_period\":600,\"reset_reason\":8},\"temperature:0\":{\"id\":0,\"tC\":22.7,\"tF\":72.8},\"wifi\":{\"sta_ip\":\"192.168.178.75\",\"status\":\"got ip\",\"ssid\":\"FRITZ!Box 7530 KI\",\"rssi\":-49},\"ws\":{\"connected\":false}}}","payloadType":"json","x":210,"y":2360,"wires":[["1bd7a6baa1b4345f"]]},{"id":"1bd7a6baa1b4345f","type":"change","z":"d1395164b4eec73e","name":"Set message payload to battery","rules":[{"t":"set","p":"temp","pt":"msg","to":"[{\"y\":null,\"series\":\"Volts\"},{\"y\":null,\"series\":\"Percentuale\"}]","tot":"json"},{"t":"set","p":"temp[0].y","pt":"msg","to":"payload.params[\"devicepower:0\"].battery.V","tot":"msg"},{"t":"set","p":"temp[1].y","pt":"msg","to":"payload.params[\"devicepower:0\"].battery.percent","tot":"msg"},{"t":"move","p":"temp","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":2280,"wires":[["10e8392c9eb3c032"]]},{"id":"10e8392c9eb3c032","type":"debug","z":"d1395164b4eec73e","name":"battery","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":650,"y":2280,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [27 December 2023 20:54 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/5 "2023-12-27T20:54:14Z")

</div>

Thanks a lot !

Very usefull, and clear, reply.

I'll deep into these arguments, and will try everything.

I didn't meet this syntax before this evening:

```
$$.payload.params."devicepower:0"

```

Thanks, thanks !!!!

---

<div class="post-metadata">

**Author:** ![realtebo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/realtebo/32/85732_2.png) [@realtebo](https://discourse.nodered.org/u/realtebo)\
**Post date:** [27 December 2023 20:56 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/7 "2023-12-27T20:56:37Z")

</div>

> [@jbudd](#):
>
> msg.payload.params

The problem was the I was extracting deep into .V and .percent but I got only some levels, up to msg.payload.params

---

<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:** [27 December 2023 21:01 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/8 "2023-12-27T21:01:02Z")

</div>

Click export, then select json, then you can select compact at the bottom before copying to clipboard. There is an option in the setting.js

```auto
flowFilePretty: false,

```

Which will set compact as default.

---

<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:** [10 January 2024 21:01 UTC](https://discourse.nodered.org/t/jsonata-how-to-access-json-keys/84065/9 "2024-01-10T21:01:49Z")

</div>

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