On Wednesday, June 14, 2017 at 6:10:55 PM UTC-4, Andre Müller wrote: > Am 14.06.2017 um 22:33 schrieb Bradley Cooper: > > I am working with an API and I get a return response in this format. > > > > > > [{"itemNumber":"75-5044","inventory":[{"warehouseCode":"UT-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"KY-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"TX-1-US","quantityAvailable":14.0000000000000},{"warehouseCode":"CA-1-US","quantityAvailable":4.0000000000000},{"warehouseCode":"AB-1-CA","quantityAvailable":1.0000000000000},{"warehouseCode":"WA-1-US","quantityAvailable":0.0000000000000},{"warehouseCode":"PO-1-CA","quantityAvailable":0.0000000000000}]}] > > > > What is the best way to read through the data? > > > > Your data looks like Json. > First use the _json_ module to convert the _string_ into a _Python data > structure_. > Then you can iterate over it. My example is a nested loop. Maybe > you've more than one element inside the list. > > > import json > > # the string looks like json > JSON_DATA = """[ > {"itemNumber":"75-5044","inventory": [ > {"warehouseCode":"UT-1-US","quantityAvailable":0.0000000000000}, > {"warehouseCode":"KY-1-US","quantityAvailable":0.0000000000000}, > {"warehouseCode":"TX-1-US","quantityAvailable":14.0000000000000}, > {"warehouseCode":"CA-1-US","quantityAvailable":4.0000000000000}, > {"warehouseCode":"AB-1-CA","quantityAvailable":1.0000000000000}, > {"warehouseCode":"WA-1-US","quantityAvailable":0.0000000000000}, > {"warehouseCode":"PO-1-CA","quantityAvailable":0.0000000000000} > ] > } > ]""" > > > # converting the json string to a Python data structure > inventory = json.loads(JSON_DATA) > > # representation in Python > #[{'inventory': [{'quantityAvailable': 0.0, 'warehouseCode': 'UT-1-US'}, > #{'quantityAvailable': 0.0, 'warehouseCode': 'KY-1-US'}, > #{'quantityAvailable': 14.0, 'warehouseCode': 'TX-1-US'}, > #{'quantityAvailable': 4.0, 'warehouseCode': 'CA-1-US'}, > #{'quantityAvailable': 1.0, 'warehouseCode': 'AB-1-CA'}, > #{'quantityAvailable': 0.0, 'warehouseCode': 'WA-1-US'}, > #{'quantityAvailable': 0.0, 'warehouseCode': 'PO-1-CA'}], > #'itemNumber': '75-5044'}] > > > # the interesting part > for items in inventory: > # get the elements in from the list > # the elements are dicts, in this case exactly one dict > print('itemNumber:', i['itemNumber']) > # nested loop iterating over the values of the key 'inventory' > for item in items['inventory']: > # the value is dict > code, qty = item['warehouseCode'],item['quantityAvailable'] > print('warehouseCode:', code, 'quantityAvailable', qty) > > > # Output > #itemNumber: 75-5044 > #warehouseCode: UT-1-US quantityAvailable 0.0 > #warehouseCode: KY-1-US quantityAvailable 0.0 > #warehouseCode: TX-1-US quantityAvailable 14.0 > #warehouseCode: CA-1-US quantityAvailable 4.0 > #warehouseCode: AB-1-CA quantityAvailable 1.0 > #warehouseCode: WA-1-US quantityAvailable 0.0 > #warehouseCode: PO-1-CA quantityAvailable 0.0 > > > Greetings > Andre
Thanks Andre, that works I just needed to convert my return to a string str(DATA) -- https://mail.python.org/mailman/listinfo/python-list