Extract Json Data with Python
Question:
My Jason Data looks like this:
{
"componentId": "SD1:1100047938",
"componentType": "Device",
"name": "WR50MS15-7938 (WR 33)",
"product": "SB 5000TL",
"productTagId": 9037,
"pvPower": 886,
"serial": "1100047938",
"specWhOutToday": 3.0909803921568626,
"specWhOutYesterday": 2.924313725490196,
"state": 307,
"totWhOutToday": 15764,
"totWhOutYesterday": 14914
}
How could i only extract:
- "state" to a separate file
- "pv-power" to a sperate file ?
Thanks !
Answers:
url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
payload={}
headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
data = json.loads(response.text)
with open('data.json', 'w') as f:
data = json.dump(data, f, indent = 2)
This is my Code for gathering the JSON Data.
I need to exctract the above mentioned Values.
After you run data = json.loads(response.text)
, the json is loaded into your data
variable as a python dictionary.
So state = data.state
and pvPower = data.pvPower
should give the info you’re after.
I’m not exactly sure what you want to do with that information, as far as extracting to a separate file. But, json.dump()
does output data to a json file.
import requests
import json
import urllib3
import sys
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fehler = '"state": 35'
urltoken = "https://172.16.63.100/api/v1/token"
urldaten = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
urlaktuell = "https://172.16.63.100/api/v1/widgets/gauge/power?componentId=Plant%3A1&type=PvProduction"
data = {
"grant_type": "password",
"username": "...",
"password": "...",
}
response = requests.post(urltoken, data, verify=False)
#print(response.json())
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt') as json_file:
data1 = json.load(json_file)
token = data1["access_token"]
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urldaten, headers=headers, data=payload, verify=False)
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/info.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
if fehler in open('/usr/lib/nagios/plugins/check_DataManager/info.txt').read():
print("Mindestens ein Wechselrichter hat einen Fehler!")
exit(1)
else:
print("Alle Wechselrichter Online!")
exit(0)
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urlaktuell, headers=headers, data=payload, verify=False)
aktuell = json.loads(response.text)
daten = aktuell["value"]
print("Aktuelle Leistung:", 0.001*daten , "KWh")
I now managed to do all i wanted like this 🙂
Well at first it ignores all Certificate Warnings i have been getting from my SMA Monitoring Device.
Then it gathers the Bearer Access token an stores it into an .txt file
After this is sends a request for json data to urldaten and urlaktuell. This Data is then stored in info.txt and ertrag.txt 🙂 In this Files it checks if there is a faulty Inverter stored 🙂
My Jason Data looks like this:
{
"componentId": "SD1:1100047938",
"componentType": "Device",
"name": "WR50MS15-7938 (WR 33)",
"product": "SB 5000TL",
"productTagId": 9037,
"pvPower": 886,
"serial": "1100047938",
"specWhOutToday": 3.0909803921568626,
"specWhOutYesterday": 2.924313725490196,
"state": 307,
"totWhOutToday": 15764,
"totWhOutYesterday": 14914
}
How could i only extract:
- "state" to a separate file
- "pv-power" to a sperate file ?
Thanks !
url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
payload={}
headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
data = json.loads(response.text)
with open('data.json', 'w') as f:
data = json.dump(data, f, indent = 2)
This is my Code for gathering the JSON Data.
I need to exctract the above mentioned Values.
After you run data = json.loads(response.text)
, the json is loaded into your data
variable as a python dictionary.
So state = data.state
and pvPower = data.pvPower
should give the info you’re after.
I’m not exactly sure what you want to do with that information, as far as extracting to a separate file. But, json.dump()
does output data to a json file.
import requests
import json
import urllib3
import sys
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fehler = '"state": 35'
urltoken = "https://172.16.63.100/api/v1/token"
urldaten = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
urlaktuell = "https://172.16.63.100/api/v1/widgets/gauge/power?componentId=Plant%3A1&type=PvProduction"
data = {
"grant_type": "password",
"username": "...",
"password": "...",
}
response = requests.post(urltoken, data, verify=False)
#print(response.json())
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt') as json_file:
data1 = json.load(json_file)
token = data1["access_token"]
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urldaten, headers=headers, data=payload, verify=False)
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/info.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
if fehler in open('/usr/lib/nagios/plugins/check_DataManager/info.txt').read():
print("Mindestens ein Wechselrichter hat einen Fehler!")
exit(1)
else:
print("Alle Wechselrichter Online!")
exit(0)
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urlaktuell, headers=headers, data=payload, verify=False)
aktuell = json.loads(response.text)
daten = aktuell["value"]
print("Aktuelle Leistung:", 0.001*daten , "KWh")
I now managed to do all i wanted like this 🙂
Well at first it ignores all Certificate Warnings i have been getting from my SMA Monitoring Device.
Then it gathers the Bearer Access token an stores it into an .txt file
After this is sends a request for json data to urldaten and urlaktuell. This Data is then stored in info.txt and ertrag.txt 🙂 In this Files it checks if there is a faulty Inverter stored 🙂