# Trying to recreate new dicitonary from loop of API returned dictionary

**URL:** <https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 20, 2023, 6:07pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322 "2023-03-20T18:07:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Salisbury](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thomas_salisbury/32/62046_2.png) [@Thomas\_Salisbury](https://forum.inductiveautomation.com/u/Thomas_Salisbury)\
**Post date:** [March 20, 2023, 6:07pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/1 "2023-03-20T18:07:03Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 20, 2023, 6:23pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/2 "2023-03-20T18:23:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Thomas\_Salisbury](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thomas_salisbury/32/62046_2.png) [@Thomas\_Salisbury](https://forum.inductiveautomation.com/u/Thomas_Salisbury)\
**Post date:** [March 20, 2023, 6:26pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/3 "2023-03-20T18:26:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thomas\_Salisbury](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thomas_salisbury/32/62046_2.png) [@Thomas\_Salisbury](https://forum.inductiveautomation.com/u/Thomas_Salisbury)\
**Post date:** [March 20, 2023, 6:26pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/4 "2023-03-20T18:26:46Z")

</div>

I am pretty sure I am mixing languages here, I'm not just trained in python

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 20, 2023, 6:35pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/5 "2023-03-20T18:35:17Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 20, 2023, 6:39pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/6 "2023-03-20T18:39:23Z")

</div>

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:

```python
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']
	]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 20, 2023, 6:44pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/7 "2023-03-20T18:44:18Z")

</div>

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):

```python
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']
	]

```

---

<div class="post-metadata">

**Author:** ![Thomas\_Salisbury](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thomas_salisbury/32/62046_2.png) [@Thomas\_Salisbury](https://forum.inductiveautomation.com/u/Thomas_Salisbury)\
**Post date:** [March 20, 2023, 6:51pm UTC](https://forum.inductiveautomation.com/t/trying-to-recreate-new-dicitonary-from-loop-of-api-returned-dictionary/71322/8 "2023-03-20T18:51:55Z")

</div>

Thank you. That was perfect
