FYI—the sample data from the output file looks wrong because the value string is not valid json. It should be like this:
"{"results":[{"name":"Camera","department":{"name":"Electronics","storeDeptId":-1},"location":{"aisle":["M.35"],"detailed":[{"zone":"M","aisle":"36","section":"2"}]},"price":{"priceInCents":49900,"isRealTime":true,"currencyUnit":"USD"},"inventory":{"quantity":3,"status":"Out of stock","isRealTime":true}}]}"
Note the ]
that is in my version of your JSON but not in yours. Once you get to valid JSON, you can use json.loads to transform that JSON string into a value that you can pull data out of:
data = json.loads(data['searchResults'])
print json.dumps(data, indent=2)
which should get you:
{
"results": [
{
"department": {
"name": "Electronics",
"storeDeptId": -1
},
"inventory": {
"status": "Out of stock",
"isRealTime": true,
"quantity": 3
},
"price": {
"priceInCents": 49900,
"isRealTime": true,
"currencyUnit": "USD"
},
"name": "Camera",
"location": {
"detailed": [
{
"aisle": "36",
"section": "2",
"zone": "M"
}
],
"aisle": [
"M.35"
]
}
}
]
}
Now, something like this will get you close to the trimmed output you want:
for coro in asyncio.as_completed(tasks, loop=loop):
try:
data, store = await coro
result = json.loads(data['searchResults'])['results'][0] #Storing retrieved json data
summary = {
'name': result['name'],
'aisle': result['location']['aisle'][0],
'status': result['inventory']['status'],
}
results[store] = summary
except (IndexError):
continue
After this, the output in your output file will look something like:
'35': { 'name': 'Camera', 'aisle': 'M.35', 'status': 'Out of stock' },