HI, I am trying to loop a returned API call from json format, into a dicitonary, and I want to loop that data and recreate a new dicitonary cleaned up for a table.
Here is my code thats not working.
def runAction(self):
orders = {}
system.perspective.print( "Page Script Started" )
client = system.net.httpClient()
response = client.get("https://api.megamation.com/clm/DL/workorder?STATUS=S&TYPE=PM&BUILDING_ID=Climatemaster", username="<>", password="<>")
i = 0
for key in response.json['_embedded']['WorkOrder']:
orders[i]['date'] = key['date']
orders[i]['building_id'] = key['building_id']
orders[i]['wo_no'] = key['wo_no']
orders[i]['work_request_description'] = key['work_request_description']
orders[i]['type'] = key['type']
i += 1
self.custom.getPMOrders = workOrders
side note, I can print through the loop just fine, but i can't seem to create a new dictionary from that same loop
Where do you define workOrders? I see self.custom.getPMOrders = workOrders
but I don't see where workOrders
itself is defined unless I missed it. Did you mean orders = {}
from the first line?
Yes, but that's not the issue. It was just a typo.
Error running action 'system.onStartup' on Home@D/root: Traceback (most recent call last): File "function:runAction", line 8, in runAction KeyError: 0
I am pretty sure I am mixing languages here, I'm not just trained in python
Ok I would suggest fixing the typo in your original post oterwise people will think similar. self.custom.getPMOrders will not show anything if you are assigning a non existent variable to it.
Looks like you are getting a key error which means you are referencing a key that is not in the dictionary ie myDict['thisKeyIsntReal']
would cause a key error if thisKeyIsntReal
doesn't exist in myDict
but I try to reference it.
I think one issue is that you don't initialize your orders[i] as a dictionary. I would add a orders[i] = {}
as the very first line in your for loop to initialize the dictionary and the I think the rest will go through (assuming that it is not they key object that is throwing the KeyError).
The 'Pythonic' way to construct a repetitive list like this would be a comprehension. I suspect that you actually want your return to be a list of objects, not deeply nested objects as your code sample implies. Try this:
def runAction(self):
system.perspective.print( "Page Script Started" )
client = system.net.httpClient()
response = client.get("https://api.megamation.com/clm/DL/workorder?STATUS=S&TYPE=PM&BUILDING_ID=Climatemaster", username="<>", password="<>")
self.custom.getPMOrders = [
{
'date': key['date'],
'building_id': key['building_id'],
'wo_no': key['wo_no'],
'work_request_description': key['work_request_description'],
'type': key['type'],
}
for key in response.json['_embedded']['WorkOrder']
]
2 Likes
You can also nest your comprehensions to remove some repetition. I don't necessarily think this is a good idea, though (it's not very flexible):
def runAction(self):
system.perspective.print( "Page Script Started" )
client = system.net.httpClient()
response = client.get("https://api.megamation.com/clm/DL/workorder?STATUS=S&TYPE=PM&BUILDING_ID=Climatemaster", username="<>", password="<>")
relevantKeys = ['date', 'building_id', 'wo_no', 'work_request_description', 'type']
self.custom.getPMOrders = [
{
key: workOrder[key]
for key in relevantKeys
}
for workOrder in response.json['_embedded']['WorkOrder']
]
Thank you. That was perfect