Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fetch only one type of key values in an object in python

so here is a object which i got from Telegram API

{'ok': True, 'result': [{'update_id': 634527149, 'message': {'message_id': 2, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641826968, 'text': 'Hi'}}, {'update_id': 634527150, 'message': {'message_id': 13, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641827128, 'text': '/start', 'entities': [{'offset': 0, 'length': 6, 'type': 'bot_command'}]}}, {'update_id': 634527151, 'message': {'message_id': 38, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641827383, 'text': '/start', 'entities': [{'offset': 0, 'length': 6, 'type': 'bot_command'}]}}, {'update_id': 634527152, 'message': {'message_id': 39, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641827627, 'text': 'Hello'}}, {'update_id': 634527153, 'message': {'message_id': 79, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641828205, 'text': '/start', 'entities': [{'offset': 0, 'length': 6, 'type': 'bot_command'}]}}, {'update_id': 634527154, 'message': {'message_id': 129, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': 5060166011, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'type': 'private'}, 'date': 1641837940, 'text': '/start', 'entities': [{'offset': 0, 'length': 6, 'type': 'bot_command'}]}}, {'update_id': 634527155, 'my_chat_member': {'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'date': 1641837952, 'old_chat_member': {'user': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'status': 'left'}, 'new_chat_member': {'user': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'status': 'member'}}}, {'update_id': 634527156, 'message': {'message_id': 130, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641837952, 'new_chat_participant': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'new_chat_member': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'new_chat_members': [{'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}]}}, {'update_id': 634527157, 'message': {'message_id': 131, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641837967, 'text': '/start@SendToTelegramX_Bot', 'entities': [{'offset': 0, 'length': 26, 'type': 'bot_command'}]}}, {'update_id': 634527158, 'message': {'message_id': 132, 'from': {'id': 874434712, 'is_bot': False, 'first_name': '1DS18TE030', 'username': 'thekumaraswamy'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641837971, 'text': '/start@SendToTelegramX_Bot', 'entities': [{'offset': 0, 'length': 26, 'type': 'bot_command'}]}}, {'update_id': 634527159, 'message': {'message_id': 133, 'from': {'id': 874434712, 'is_bot': False, 'first_name': '1DS18TE030', 'username': 'thekumaraswamy'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641837975, 'text': '/start@SendToTelegramX_Bot', 'entities': [{'offset': 0, 'length': 26, 'type': 'bot_command'}]}}, {'update_id': 634527160, 'message': {'message_id': 134, 'from': {'id': 874434712, 'is_bot': False, 'first_name': '1DS18TE030', 'username': 'thekumaraswamy'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838036, 'text': '/gamble', 'entities': [{'offset': 0, 'length': 7, 'type': 'bot_command'}]}}, {'update_id': 634527161, 'message': {'message_id': 140, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838331, 'text': '/daan 50', 'entities': [{'offset': 0, 'length': 5, 'type': 'bot_command'}]}}, {'update_id': 634527162, 'message': {'message_id': 142, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838571, 'reply_to_message': {'message_id': 141, 'from': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838464, 'text': 'Day 16 #100DaysOfCode #151daysofcode \n- Revised react life cycle methods\n- Started Learning more about styling using CSS.\n\n#webdevelopment #reactjs\n@NikhilRNaik2', 'entities': [{'offset': 7, 'length': 14, 'type': 'hashtag'}, {'offset': 22, 'length': 14, 'type': 'hashtag'}, {'offset': 123, 'length': 15, 'type': 'hashtag'}, {'offset': 139, 'length': 8, 'type': 'hashtag'}, {'offset': 148, 'length': 13, 'type': 'mention'}]}, 'text': 'This is the twitter bot I made Today🥳🥳'}}, {'update_id': 634527163, 'message': {'message_id': 143, 'from': {'id': 874434712, 'is_bot': False, 'first_name': '1DS18TE030', 'username': 'thekumaraswamy'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838656, 'reply_to_message': {'message_id': 142, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838571, 'text': 'This is the twitter bot I made Today🥳🥳'}, 'text': '🔥🔥'}}, {'update_id': 634527164, 'message': {'message_id': 144, 'from': {'id': 820217319, 'is_bot': False, 'first_name': '1DS18TE039 - Nikhil R Naik'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838981, 'reply_to_message': {'message_id': 142, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838571, 'text': 'This is the twitter bot I made Today🥳🥳'}, 'text': 'What it does'}}, {'update_id': 634527165, 'message': {'message_id': 145, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641839096, 'reply_to_message': {'message_id': 141, 'from': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838464, 'text': 'Day 16 #100DaysOfCode #151daysofcode \n- Revised react life cycle methods\n- Started Learning more about styling using CSS.\n\n#webdevelopment #reactjs\n@NikhilRNaik2', 'entities': [{'offset': 7, 'length': 14, 'type': 'hashtag'}, {'offset': 22, 'length': 14, 'type': 'hashtag'}, {'offset': 123, 'length': 15, 'type': 'hashtag'}, {'offset': 139, 'length': 8, 'type': 'hashtag'}, {'offset': 148, 'length': 13, 'type': 'mention'}]}, 'text': 'If you give twitter username it will fetch the recent tweets and send you as message from Telegram bot'}}, {'update_id': 634527166, 'message': {'message_id': 146, 'from': {'id': 5060166011, 'is_bot': False, 'first_name': 'Mosquito', 'username': 'TheMosquitoo', 'language_code': 'en'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641839110, 'reply_to_message': {'message_id': 141, 'from': {'id': 5082767515, 'is_bot': True, 'first_name': 'Send To Telegram', 'username': 'SendToTelegramX_Bot'}, 'chat': {'id': -646604598, 'title': '151 days of coding', 'type': 'group', 'all_members_are_administrators': True}, 'date': 1641838464, 'text': 'Day 16 #100DaysOfCode #151daysofcode \n- Revised react life cycle methods\n- Started Learning more about styling using CSS.\n\n#webdevelopment #reactjs\n@NikhilRNaik2', 'entities': [{'offset': 7, 'length': 14, 'type': 'hashtag'}, {'offset': 22, 'length': 14, 'type': 'hashtag'}, {'offset': 123, 'length': 15, 'type': 'hashtag'}, {'offset': 139, 'length': 8, 'type': 'hashtag'}, {'offset': 148, 'length': 13, 'type': 'mention'}]}, 'text': 'Just like this'}}]}

So in this object i only need ‘text’ key and value.
but the thing is the key ‘text’ is present only few times in object…
I need to scrap only that data…Please help me out.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Iterate over dict in 'result', then reach message > text

for item in x['result']:
    if 'message' in item and 'text' in item['message']:
        print(item['message']['text'])

Hi
/start
/start
Hello
/start
/start
/start@SendToTelegramX_Bot
/start@SendToTelegramX_Bot
/start@SendToTelegramX_Bot
/gamble
/daan 50
This is the twitter bot I made Today🥳🥳
🔥🔥
What it does
If you give twitter username it will fetch the recent tweets and send you as message from Telegram bot
Just like this
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading