1

EDIT: added actual JSON object and code for review

I have the JSON in this format (just this hierarchy, assume the JSON is working)

{u'kind': u'calendar#events', u'defaultReminders': [{u'minutes': 60, u'method': u'email'},             {u'minutes': 60, u'method': u'sms'}], u'items': [{u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'date': u'2013-07-27'}, u'created': u'2013-06-15T19:06:13.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=OTNpZHVpNWViMTVrbmw4YWF2bmcyaXUxbWMgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-15T19:06:13.635Z', u'summary': u'white water rafting', u'start': {u'date': u'2013-07-26'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTMyMzE3MzYzNTAwMA"', u'transparency': u'transparent', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'93idui5eb15knl8aavng2iu1mc'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-24T13:00:00-05:00'}, u'created': u'2013-06-21T18:32:08.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=Njdjam5xYm9xM3Voa3JkNXVkb2dkNXBvZzQgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:32:08.192Z', u'summary': u'camp counselor', u'start': {u'dateTime': u'2013-06-24T09:00:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTUyODE5MjAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'67cjnqboq3uhkrd5udogd5pog4'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-25T09:00:00-05:00'}, u'created': u'2013-06-21T18:32:21.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=b2t0bGZwNjUwM3UwNDZ1ZXE1ZHFtbjA3cXMgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:32:21.673Z', u'summary': u'front desk', u'start': {u'dateTime': u'2013-06-25T05:45:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTU0MTY3MzAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'oktlfp6503u046ueq5dqmn07qs'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-25T10:00:00-05:00'}, u'created': u'2013-06-21T18:32:29.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=MmoxMzlmZWMxbmljaGJ1YTJ0MXAxNjFhdTggbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:32:29.802Z', u'summary': u'camp counselor', u'start': {u'dateTime': u'2013-06-25T09:00:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTU0OTgwMjAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'2j139fec1nichbua2t1p161au8'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-26T09:00:00-05:00'}, u'created': u'2013-06-21T18:32:48.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=MHYxOWRiMW9wYjh1ZW1ucWJlMm04ZjVzZDQgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:32:48.507Z', u'summary': u'fitness floor', u'start': {u'dateTime': u'2013-06-26T05:45:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTU2ODUwNzAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'0v19db1opb8uemnqbe2m8f5sd4'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-26T13:00:00-05:00'}, u'created': u'2013-06-21T18:32:55.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=bzcxdDh0MnBqZzVmbDA3NWlwbzBlMTRkaWsgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:32:55.986Z', u'summary': u'camp counselor', u'start': {u'dateTime': u'2013-06-26T09:00:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTU3NTk4NjAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'o71t8t2pjg5fl075ipo0e14dik'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-27T13:00:00-05:00'}, u'created': u'2013-06-21T18:33:11.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=ZGZmbzZlbjRlZm5iamIwdG0wNmV2OTJlbjQgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:33:11.511Z', u'summary': u'camp counselor', u'start': {u'dateTime': u'2013-06-27T09:00:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTU5MTUxMTAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'dffo6en4efnbjb0tm06ev92en4'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-28T09:00:00-05:00'}, u'created': u'2013-06-21T18:33:21.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=MnA2Yzd1MjRrYnYzMGFjdW9qYmgybmU3OTQgbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:33:21.986Z', u'summary': u'fitness floor', u'start': {u'dateTime': u'2013-06-28T05:45:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTYwMTk4NjAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'2p6c7u24kbv30acuojbh2ne794'}, {u'status': u'confirmed', u'kind': u'calendar#event', u'end': {u'dateTime': u'2013-06-28T13:00:00-05:00'}, u'created': u'2013-06-21T18:33:50.000Z', u'iCalUID': u'[email protected]', u'reminders': {u'useDefault': True}, u'htmlLink': u'https://www.google.com/calendar/event?eid=Nm91djQzb3FpOTRvZ3ZxYW0wbGRoNXBrNDggbG9nYW5AbG9nYW5oZW5zb24uY29t', u'sequence': 0, u'updated': u'2013-06-21T18:33:50.756Z', u'summary': u'camp counselor', u'start': {u'dateTime': u'2013-06-28T09:00:00-05:00'}, u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/MTM3MTgzOTYzMDc1NjAwMA"', u'organizer': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'creator': {u'self': True, u'displayName': u'Logan Henson', u'email': u'[email protected]'}, u'id': u'6ouv43oqi94ogvqam0ldh5pk48'}], u'updated': u'2013-06-22T19:58:01.388Z', u'summary': u'[email protected]', u'etag': u'"CxZm3kc9PAmVWyOavlumf_6aLhg/8Ps07gFrFoxf7ptkCmaNe4n6omU"',         u'timeZone': u'America/Chicago', u'accessRole': u'owner'}    

and am successfully extracting the "camp counselor" and "u'dateTime':u'2013-06-24T09:00:00-05:00'" but would like to be able to extract the dateTime value...instead of the full start value.

what I have now is: (EDITED for what I need) I am trying to get every calendar items summary + start + end, and have been successful but need to get the specific datetime instead of the dict object. The following gives keyerror at dateTime

tot = []
val = test["items"]
for a in val:
    Item = calItem(a["summary"], a["start"]["dateTime"], a["end"])
    tot.append(Item)

so is there a way to get the secondary JSON dict value or start? i.e. can I do something to the effect of a["start"][keyvalue I want]?

Thanks!

0

2 Answers 2

6

Yes, you can stack it (if that is what you're asking):

>>> d['items'][1]['start']['dateTime']
u'2013-06-24T09:00:00-05:00'
Sign up to request clarification or add additional context in comments.

7 Comments

I cannot get that to work by doing: a['start']['dateTime'] I am getting a KeyError at dateTime
@user2016117 Impossible. Unless your question code was incorrect, there is a dictionary with a key of dateTime in start. You'll have to edit your question and post your full code.
I have added the actual JSON object I am using, I hope it sheds some light, as I thought your solution would work when I initially tried to work on this.
@user2016117 there are many camp councillors in your dict. Is there one specifically you need?
I have edited my code sample to show the purpose of my code, I have already simplified the JSON to d['items'] and am working with the individual objects inside items. (dicts). Therefore using your answer I thought a['start']['dateTime'] would return what I need, but it keyErrors..I'm very sorry if I've only muddled the question further!
|
0

My final solution is this for those new to JSON like myself:

Item = calItem(a["summary"], a['start'].get('dateTime', a['start'].get('date')), a["end"].get("dateTime", a['start'].get('date')))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.