skip to Main Content

So I’m working around with a JSON the Telegram API is giving me. I’m trying to get the message value into a string.

Here is my Json.

{
    "ok": true,
    "result": [
        {
            "update_id": 855636291,
            "message": {
                "message_id": 71,
                "from": {
                    "id": 1337,
                    "first_name": "*",
                    "last_name": "*",
                    "username": "*"
                },
                "chat": {
                    "id": 1337,
                    "first_name": "*",
                    "last_name": "*",
                    "username": "*"
                },
                "date": 1435987802,
                "text": "Testing"
            }
        }
    ]
    }

Code I’m trying to use to get the value. (Using Requests btw)

content = json.loads(r)
msg = content['result'][0]['message'][0]['text]

However, it doesn’t work. I managed to retrieve the update_id with content['result'][0]['update_id'] but I have no idea on how to retrieve the text.

Thanks in advance!

2

Answers


  1. message is not a list, text is a key directly in it:

    msg = content['result'][0]['message']['text']
    
    Login or Signup to reply.
  2. Remove the [0] after ['message']

    msg = content['result'][0]['message']['text']
    

    Here message is a dictionary and text is a key which is in that dictionary. Just access text as you would normally in a dictionary.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search