Python code to extract item from json

Posted on
Tue Apr 19, 2022 10:48 am
zoff offline
Posts: 47
Joined: Jan 21, 2013

Python code to extract item from json

Code: Select all
"inputs":[{"input":1,"event":"","event_cnt":23}]

How do I extract the number 1 from the above json using Python?

thanks

Posted on
Tue Apr 19, 2022 1:17 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python code to extract item from json

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Apr 20, 2022 3:42 am
zoff offline
Posts: 47
Joined: Jan 21, 2013

Re: Python code to extract item from json

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.


Thank you! That works, but I can't wrap my head around how it works. If you don't mind can you give a brief explanation.

The string I posted above is what I extracted from the full json result below..
Code: Select all
{"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}


To do that I used this code snippet I found online.
Code: Select all
import urllib
import time
import requests

r = requests.get(url='http://192.168.8.105/status')  #window 1
res = r.json()
cp1 = (res["inputs"])

Posted on
Wed Apr 20, 2022 4:50 am
zoff offline
Posts: 47
Joined: Jan 21, 2013

Re: Python code to extract item from json

UPDATE: I need to restate my question because I can't make the 2 snippets above work. Sorry.
The question then becomes, how can I extract the 1 from the response below.

Code: Select all
{"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}

Posted on
Wed Apr 20, 2022 4:58 am
zoff offline
Posts: 47
Joined: Jan 21, 2013

Re: Python code to extract item from json

UPDATE 2: I spoke too soon, I now got it to work. Thank for your help FlyingDiver.

This is the code that I'm using to get the results that I need incase anyone is interested.

Code: Select all
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))

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 15 guests