I'm using PRTG HTTP API to pull sensors information, my goal is to filter up the sensors that have this value
<status>Down</status>
So far I've read a lot of references in Stack and Googling but I have not been able to apply none of this solutions, so pasting all the codes I've tried would be futile however the one that makes more sense to me is this one
The structure of the JSON is like this
{
"prtg-version": "19.1.49.1966",
"treesize": 17701,
"status%3Ddown": [
{
"group": "SOME GROUP",
"device": "SOME SWITCH",
"sensor": "Uptime",
"status": "Down",
"status_raw": 5
},
{
"group": "SOME GROUP",
"device": "SOME SWITCH",
"sensor": "System Health Memory",
"status": "Up",
"status_raw": 3
},
]
}
Now here's the code
import json
import requests
url = "https://prtg.c3ntro.com/api/table.jsoncontent=status=down&username=usr&passhash=hash&count=100"
response = requests.get(url)
data = response.json()
d = data
result_dict = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
print(result_dict)
#Code has been fixed and now it works
A list comprehension should work:
[d for d in data['status%3Ddown'] if d['status'] == 'Down']
returns all entries in "status%3Ddown"
list that contain "status": "Down"
:
[{'device': 'SOME SWITCH',
'group': 'SOME GROUP',
'sensor': 'Uptime',
'status': 'Down',
'status_raw': 5}]