Learn Why to Code

A brief introduction to practical programming

« Previous: Pick out the text of a tweet Next: Loop and repeat »

Pick Out the Text From Many Tweets

The problem: How do we work with the JSON for a page of tweets?
The solution: Because a page of tweets can contain many tweets, they are stored as arrays

Lesson content is currently in draft form.

In the last chapter, we dealt with the Twitter user data object, which when parsed, is a Ruby Hash.

But the data object for a page of tweets is different:

1
2
3
4
5
6
7
8
9
10
require 'rubygems'
require 'json'
require 'httparty'

url = "http://nottwitter.danwin.com/statuses/RoyBlunt/3/user_timeline.json"
json_str = HTTParty.get(url).body
tweets_obj = JSON.parse(json_str)

puts tweets_obj.class
# => Array

If you look at the contents of the JSON in tweets_obj, you should see something that looks like a collection of Hash objects. But what exactly is this Array that holds them?

The Array object

In Python, arrays are known as lists, which may be the easier way to think of this type of collection. Like the Hash, the Array is a collection of objects. However, instead of using keys, the objects in Array can be accessed (using the same kind of square-bracket notation) in a numerically sequential order.

Here’s a sample Array:

1
2
3
4
5
6
arr = ['a', 'beta', 'cow', 'dog']
puts arr[0]
#=> a

puts arr[2]
#=> cow

We’ll examine arrays in greater detail later. For now, note:

  1. Array items are accessed with integers. They don’t use strings or other types of data-objects (as a Hash can)

  2. A Ruby array is zero-indexed. That means the first element is found at the address of 0. The second element is at 1, and so forth.

Collections inside collections

The string and number datatypes are primitive compared to the collection datatypes. As an example, the tweets_obj array contains a bunch of objects, and if you inspect each object, you’ll see that each is itself a Hash:

1
2
3
4
5
6
7
x =  tweets_obj[0]
puts x.class
#=> Hash

y = tweets_obj[0]
puts y.class
#=> Hash

In other words, each individual Tweet is represented as a Hash

Exercise

Look over the downloaded JSON to see the structure of the Tweet hash object. Print out the following:

  1. The text of the first tweet
  2. The number of retweets of the eighth tweet
  3. The date of the 14th tweet

Answer

1
2
3
puts tweets_obj[0]['text']
puts tweets_obj[7]['retweet_count']
puts tweets_obj[14]['created_at']

Bonus: What happens when you try to access an address outside of an Array’s range? Use the length method to find the number of items in an array.

Comments