Python Tutorial: Understanding Twitter JSON

Learn how Twitter's JSON data is structured and how to parse it in Python. The tutorial covers core tweet fields like text and creation time, nested objects like User and Place, and how to access nested values using dictionary keys like tweet['user']['screen_name'].
Chapters
Great job. Now that you've collected some Twitter data, let's dig into the structure of the data. In the examples which we are working with in this course, the information is returned in JavaScript object notation, or JSON. JSON is a special data format which is both human readable and is easily transferred between machines. JSON is structured a lot like Python objects and is composed of a combination of dictionaries and lists. Understanding the Twitter JSON is critical to knowing how to analyze Twitter data. There is a lot of data in a single Twitter JSON. For instance, in a single original tweet that is a tweet that is not a retweet or a quoted tweet, you have foundational information like the text when it was created and the unique tweet id. You also have information like how many retweets or favorites it has at the time of collection, what language it's in, if it's a reply to a tweet, and to which tweet and to which user. More importantly, the Twitter JSON contains several important child JSON objects. These are like dictionaries stored in other dictionaries. The important ones which we'll cover in this course are User place and an extended tweet. User contains all the useful information you'd want to know about the user who tweeted, including their name, their Twitter handle, their Twitter bio, their location, and if they're verified. Place contains information on the geolocation of the tweet, and we'll get into that in chapter four. Extended Tweet contains the full text of a tweet, which is over 140 characters in length. When a tweet is a retweet or contains a quoted tweet, the whole of that tweet will be contained with the Twitter JSON. For retweets, that tweet will be stored in retweetedstatus and for a quoted tweet in quoted status. Let's start exploring the Twitter JSON by loading a single tweet. We'll use the open and read methods to load JSON file into a JSON object. Then we'll use the JSON package and the loads method to convert the JSON into into a Python dictionary. Lastly, we access the value of interest by using its appropriate key child Twitter JSON can be accessed as nested dictionaries. Let's look at the user object. To access the user's handle, we use the user key, then the screen name key. We can do the same with name to show the user's displayed name and created at to show when the user created their Twitter account. Let's practice working with Twitter JSON.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







