- Code: Select all
"inputs":[{"input":1,"event":"","event_cnt":23}]
How do I extract the number 1 from the above json using Python?
thanks
"inputs":[{"input":1,"event":"","event_cnt":23}]
import json
the_dict = json.loads('{"inputs":[{"input":1,"event":"","event_cnt":23}]}')
value = the_dict["inputs"][0]["input"]
FlyingDiver wrote:
- Code: Select all
import json
the_dict = json.loads('{"inputs":[{"input":1,"event":"","event_cnt":23}]}')
value = the_dict["inputs"][0]["input"]
I had to clean up the input string, it wasn't actually valid json.
{"wifi_sta":{"connected":true,"ssid":"zoffPT","ip":"192.168.8.105","rssi":-71},"cloud":{"enabled":true,"connected":true},"mqtt":{"connected":false},"time":"17:10","unixtime":1650384634,"serial":149,"has_update":false,"mac":"C45BBE5727DD","cfg_changed_cnt":6,"actions_stats":{"skipped":0},"relays":[{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"timer"}],"meters":[{"power":0.00,"is_valid":true}],"inputs":[{"input":1,"event":"","event_cnt":23}],"ext_sensors":{},"ext_temperature":{},"ext_humidity":{},"update":{"status":"idle","has_update":false,"new_version":"20220209-092750/v1.11.8-g8c7bb8d","old_version":"20220209-092750/v1.11.8-g8c7bb8d"},"ram_total":50864,"ram_free":36708,"fs_size":233681,"fs_free":147839,"uptime":270068}
import urllib
import time
import requests
r = requests.get(url='http://192.168.8.105/status') #window 1
res = r.json()
cp1 = (res["inputs"])
{"wifi_sta":{"connected":true,"ssid":"zoffPT","ip":"192.168.8.105","rssi":-71},"cloud":{"enabled":true,"connected":true},"mqtt":{"connected":false},"time":"17:10","unixtime":1650384634,"serial":149,"has_update":false,"mac":"C45BBE5727DD","cfg_changed_cnt":6,"actions_stats":{"skipped":0},"relays":[{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"timer"}],"meters":[{"power":0.00,"is_valid":true}],"inputs":[{"input":1,"event":"","event_cnt":23}],"ext_sensors":{},"ext_temperature":{},"ext_humidity":{},"update":{"status":"idle","has_update":false,"new_version":"20220209-092750/v1.11.8-g8c7bb8d","old_version":"20220209-092750/v1.11.8-g8c7bb8d"},"ram_total":50864,"ram_free":36708,"fs_size":233681,"fs_free":147839,"uptime":270068}
import urllib
import time
import requests
r = requests.get(url='http://192.168.8.105/status') #window 1
res = r.json()
cp1 = (res["inputs"][0]["input"])
indigo.server.log("json result "+ str(cp1))
Users browsing this forum: No registered users and 15 guests