0

I am working on learning to code an application using the SLACK API and Python 3. It provides a JSON response object, only I am finding that my knowledge in list/dictionary comprehension is severely lacking. I am really having a hard time figuring out how to work the syntax to extract the particular information from the following JSON object:

"channels": [
    {
        "id": "C73HHT2TZ",
        "name": "general",
        "is_channel": true,
        "created": 1465530333,
        "creator": "V35HUT43C",
        "is_archived": false,
        "is_general": true,
        "has_pins": false,
        "is_member": true,
        "last_read": "1467334012.000165",
        "latest": {
            "type": "message",
            "user": "USLACKBOT",
            "text": "Good bye. See you when you return...",
            "ts": "1467334012.000165"
        },
        "unread_count": 0,
        "unread_count_display": 0,
        "members": [
            "X95HUT43C",
            "Ad5RQAAHZ",
            "X45SY5VEH"
        ]

In the above JSON object, I am able to retrieve the entire raw 'Channel' dump using:

response = slack.rtm.start() 
listening = response.body['channels']
for i in listening:
    print(i)

I have tried this as well to no avail:

response = slack.rtm.start() 
listening = response.body['channels'][0]
for i in listening:
    print(i)

but this just gets me the following:

id
is_archived
is_general
purpose
name
is_channel
unread_count
members
has_pins
created
topic
creator
unread_count_display
latest
is_member
last_read code here

I am getting warmer, but unfortunately this is just a mess and I really can't seem to wrap my head around how to figure out how to get to the info in the following path:

'channels': > 'latest': > 'text': > and ultimately the value of 'text'

How would I go about doing this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
vaylain
  • 67
  • 2
  • 8
  • Just keep going: `text = response.body['channels'][0]['latest']['text']`. JSON just becomes nested lists (arrays) and dictionaries (objects). – jonrsharpe Jul 01 '16 at 09:10
  • Oh, my goodness. It was that simple?!? I must be really tired. Thank you very much. I feel a little silly now but I am sure this post will help others as well. – vaylain Jul 01 '16 at 09:14
  • @jonrsharpe you can post it as an answer. – Alexander Trakhimenok Jul 01 '16 at 09:18
  • @vaylain: you were looping over the first `channels` dictionary in your list, and that's a dictionary. Iterating over a dictionary gives you the keys, which is why you see those all printed out. – Martijn Pieters Jul 01 '16 at 09:19
  • How do I indicate this post as being answered? Sorry, obviously I do not normally ask for help. – vaylain Jul 01 '16 at 09:23
  • _"Click the green outlined checkmark to the left of the answer that solved your problem. This marks the answer as 'accepted', and by extension the question as 'has an accepted answer'."_ - But I do not see the green arrow. Sorry, otherwise I'd have closed the thread and provided jonrsharpe with his proper credit for helping me out. – vaylain Jul 01 '16 at 09:32
  • You can't accept an answer unless someone posts one; these are just comments! – jonrsharpe Jul 01 '16 at 09:52
  • Oh, well thank you once again for the help sir. – vaylain Jul 01 '16 at 10:02

0 Answers0