'Mapping from a specific point in Microsoft Graph API, Python
I've been beating my head against the wall for a couple of days now and can't quite come up with an answer.
Inside of the Microsoft Graph API, when you call for a specific type of email data it sends a JSON with a 'value' read at the top level.
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('938381cd-71f5-4a3d-a381-0a59a721948a')/messages",
"value": [
{
"@odata.etag": "W/\"CQAAABYAAAD0YZL2mrEQQpwOvq9h/XWNAAACpJ2E\"",
"bccRecipients": [],
"body": {
I'm attempting to dump the JSON into a dict and go into the value key to be able to get to the data I actually need.
print('\nGet user unread messages -> https://graph.microsoft.com/v1.0/me/messages?$filter=isRead ne true')
user_MAIL = session.get(api_endpoint('me/messages?$filter=isRead ne true'))
{len(user_MAIL.text)}\n')
text = json.dumps(user_MAIL.json(), indent=4, sort_keys=True)
The issue I keep running into is I can't figure out how to access the 'value' part. In Javascript I know I could just do something like a .map, but I've attempted several things here and can't seem to find an answer.
All I need to do is be able to enter the data from the value, and be able to list the keys and values of everything within 'value'.
Solution 1:[1]
I accidentally found the answer.
If you do a for loop like this:
for key in range(length):
print (json_object['value'][key]['id'])
print (28* ' ', "BREAK")
You're able to access the top layer and grab exactly what you want, no matter how many loops you have.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Keith Lawson |
