[berry] How to access nested json objects. #22540
-
Hi there, I have written a small script to publish Zigbee device events for devices running ESPEasy. I wonder if there is a way to handle this without converting the map to a string. Here is a demo json: #
# SetOption111 1
# 1 = use frequency output for buzzer pin instead of on/off signal, for piezo buzzers
#
# SetOption83 1
# Uses Zigbee device friendly name instead of 16 bits short addresses
#
# Set friendly names:
# ZbName <device>,<name>
#
#######################################################################################
# change broadcast ip and udp port here
var broadcast_ip = "192.168.1.255"
var udp_port = 8266
#######################################################################################
import string
u = udp()
def send_to_espeasy(payload_json)
var p0 = str(payload_json)
var i=string.find(p0,":")
var p1=string.split(p0,i-1)
var p2 = p1[0]
i=string.find(p2,"'")
p1 = string.split(p2,i+1)
var object_name = p1[1]
var nested_object = payload_json.find(object_name)
if nested_object.find("Name")
var name = str(nested_object.find("Name"))
var sendvalue = ""
print(name)
# temp/hum sensor:
if nested_object.find("Humidity") != nil && nested_object.find("Temperature") != nil # does the payload contain 'both' fields
var temp = str(nested_object.find("Temperature"))
var hum = str(nested_object.find("Humidity"))
sendvalue = temp + "," + hum
print("temp/hum")
# windowopen:
elif nested_object.find("ZoneStatusChange") != nil
sendvalue = str(nested_object.find("ZoneStatusChange"))
print("window")
# buttons:
elif nested_object.find("LidlPower") != nil
sendvalue = str(nested_object.find("LidlPower"))
print("button")
else
print("not in list")
sendvalue = ""
end
if sendvalue != ""
var event= "event,zb_" + name + "=" + sendvalue
print(event)
u.begin("", 0)
u.send(broadcast_ip, udp_port, bytes().fromstring(event))
end
end
end
tasmota.add_rule("ZbReceived",send_to_espeasy) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 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.
I'd recommend against doing any JSON parsing at the string level, to instead do all the processing at the
map
level, much less code than trying to take apart strings."Discovering" what elements are in a map, including nested maps, is based on the
keys()
function on a map.If you simply want the first key:
object_name = payload_json.keys()()
What happens here is that
keys()
returns an iterator function, and calling that function once will return the first available value.You can also iterate through a plain map with a construct like:
And here's an example of a more generalized construct for tra…