1

In python, I am accessing an API that returns a list of alerts like so:

{
  "items": [
    {
      "AlertID": 0,
      "Code": 0,
      "Source": "string",
      "Title": "string",
      "Severity": "Information",
      "Created": "2018-10-29T14:57:05.639Z",
      "ThresholdValue1": "string",
      "ThresholdValue2": "string",
      "ThresholdValue3": "string",
      "ThresholdValue4": "string",
      "ThresholdValue5": "string",
      "SnoozedEndDate": "2018-10-29T14:57:05.639Z",
      "DeviceGuid": "string",
      "AdditionalInfo": "string",
      "Archived": true,
      "AlertCategoryID": "Hardware",
      "ArchivedDate": "2018-10-29T14:57:05.639Z",
      "TicketID": 0,
      "AlertMessage": "string",
      "DeviceName": "string",
      "CustomerID": 0,
      "CustomerName": "string",
      "MessageTemplate": "string",
      "FolderID": 0,
      "PollingCyclesCount": 0
    }
  ],
  "totalItemCount": 0,
  "page": 0,
  "itemsInPage": 0,
  "totalPages": 0,
  "prevLink": "string",
  "nextLink": "string"
}

This will return a list of 20 alerts. If I wanted to print all the details of any alert that has the value 'false' for the key 'Archived', what would be the best way to go about that? I only need to see details about current alerts, not archived ones. The API also can return details if i pass in the specific AlertID as well, this specific url gives me a list of all alerts.

4 Answers4

0
for alert in result['items']:
    if alert.get('Archived') is False:
        print(alert)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its [long-term value](https://meta.stackexchange.com/q/114762/206345) by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – sepehr Oct 29 '18 at 16:07
0

This should be a straightforward filter:

alerts = api_response['items']

live_alerts = [alert for alert in alerts if not alert['Archived']]

This will give you a list of all the item bodies where Archived is false. You can write another function to process them:

def process_alert(alert):
    print(alert)
    if alert['Severity'] = 'really serious alert':
        email_support(alert)

for alert in live_alerts:
    process_alert(alert)
munk
  • 12,340
  • 8
  • 51
  • 71
  • Thank you! I will try this shortly. Would you be able to explain the `live_alerts = [alert for alert in alerts if not alert['Archived']]` line? I'm new to this and haven't seen a statement like this. – Jarrett Duskey Oct 29 '18 at 15:40
  • @JarrettDuskey this is called a list comprehension. See: https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ for a tutorial. The official docs are here: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions The tl;dr is it's a one liner to create a new list from some other collection. – munk Oct 29 '18 at 15:51
0

You should simply, assuming you have it as a json object, be able to

if json_var["items"]["archived"] == false:
    print <whatever info you want>

see: Parsing values from a JSON file?

ESCE
  • 123
  • 1
  • 6
0

Try this? I don't think the other works due to items is a list containing one dictionary.

print('\n'.join([e for e in result if not e['items'][0]['Archived']])
Rocky Li
  • 5,641
  • 2
  • 17
  • 33