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 extract specific text from JSON response

I am using the Twitter API to source tweets, and am using their provided GitHub example. However, the tweets are given in a JSON format. How can I extract one part (specifically the text and tag) and place it into either a list, dataframe (most ideal option), dict etc?

    for response_line in response.iter_lines():
    if response_line:
        json_response = json.loads(response_line)
        tweets.append(json_response)

Upon viewing the ‘tweets’ list, the data uses the following format:

{'data': {'id': '1562362833374945281',
'text': 'Waiting 😃......'},
'matching_rules': [{'id': '1562362617708027906', 'tag': 'bitcoin'}]}

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 :

json.loads() returns a dictionary. So e.g. to get the text:

json_response = json.loads(response_line)
text = json_response.get("data").get("text")
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