[berry] How to access nested json objects. #22540
Replies: 1 comment 2 replies
-
I'd recommend against doing any JSON parsing at the string level, to instead do all the processing at the "Discovering" what elements are in a map, including nested maps, is based on the You can also iterate through a plain map with a construct like:
And here's an example of a more generalized construct for traversing multiple nested map levels recursively, in this case calling a function on each leaf value:
An example of things you can do shorter would be:
Same results with:
Using f-strings can be nice for building strings, without explicit string conversions, and you could also apply a formatting for more control over details like decimals. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I have written a small script to publish Zigbee device events for devices running ESPEasy.
My script works fine. But I couldn't get the first field and therefore couldn't get to the nested object for further processing when handling this as a map.
The workaround is to convert the json map to a string and extract the field name to use with find() to get the nested object.
I wonder if there is a way to handle this without converting the map to a string.
I would also appreciate constructive criticism on my code as this is my first time using berry.
Here is a demo json:
{"TH1":{"Device":"0x96E4","Name":"TH1","Temperature":20.2,"Humidity":47.1,"Endpoint":1,"LinkQuality":51}}
And here is the script:
Beta Was this translation helpful? Give feedback.
All reactions