JSONata, how to access json keys?

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


{
  "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


[
   {
       "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

[
  {
    "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 ?

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

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

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

What's the problem?

1 Like

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

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

[{"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":[]}]
1 Like

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 !!!!

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

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

flowFilePretty: false,

Which will set compact as default.

1 Like

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