- Learn about JSON data 2.Practice iterating over nested hashes
The Bachelor is a dating show that has celebrated over 19 seasons. As it turns out, your best friend is obsessed with The Bachelor and keeps asking you really strange questions, like, "What was the name of that girl who was a cruise ship singer?".
You decided to scrape (more on scraping in an upcoming unit) Wikipedia to get all the info on seasons 9 - 19 to help her answer these burning questions. Your task now is to create methods that navigate through this massive amount of data to find answers to her questions.
The data you scraped is a hash, where the keys are the season number and the values are contestant arrays. Within these arrays, each contestant has their own hash with the following key, value pairs:
- Name
- Age
- Hometown
- Status (ex. values are "Winner", "Eliminated Week 2", etc.)
- Occupation
Here's an example of two seasons, each with only two contestants:
{
"season 30": [
{
"name": "Beth Smalls",
"age": "26",
"hometown": "Great Falls, Virginia",
"occupation":"Nanny/Freelance Journalist",
"status": "Winner"
},
{
"name": "Becca Tilley",
"age": "27",
"hometown": "Shreveport, Louisiana",
"occupation": "Chiropractic Assistant",
"status": "Eliminated Week 8"
}
],
"season 29": [
{
"name": "Ashley Yeats",
"age": "24",
"hometown": "Denver, Colorado",
"occupation":"Dental Assitant",
"status": "Winner"
},
{
"name": "Sam Grover",
"age": "29",
"hometown": "New York, New York",
"occupation": "Entertainer",
"status": "Eliminated Week 6"
}
]
}
Take a look at spec/fixtures/contestants.json
. This is the data that the spec will pass to your arguments, so get familiar with it.
JSON, or Javascript Object Notation, is a method of writing data that is language-independent but uses conventions shared by many programming languages in order to be widely readable/writable. For this reason, JSON is considered a data-interchange language.
JSON is structured as a collection of name/value pairs enclosed in curly braces––just like a ruby hash! Values can be ordered lists––just like arrays! The data object in spec/fixtures/contestants.json
that you'll be operating on looks and behaves just like a Ruby hash. You'll encounter JSON much more in the future when you begin working with scraping data from websites, getting data from APIs and even building your own APIs.
Run your testing suite to get started. Then, follow the test output together with the instructions below to solve this one.
- Build a method,
get_first_name_of_season_winner
, that takes in two arguments, a hash calleddata
(i.e. the data structure described above), and a season. The method should return the first name of that season's winner. Hint: you'll need to do some string manipulation to return only the first name of the winning lady.
- Think about how you will iterate through the hash to get to the level that contains a contestant's status.
- How will you check to see if a contestant's status equals "Winner"?
- Build a method,
get_contestant_name
, that takes in the data hash and an occupation string and returns the name of the woman who has that occupation. - Build a method,
count_contestants_by_hometown
, that takes in two arguments––the data hash and a string of a hometown. This method should return a counter of the number of contestants who are from that hometown.
- How will you keep track of contestants from a particular hometown? Think back to our looping lessons in which we set a counter variable equal to 0 and incremented that counter under certain conditions.
- Build a method
get_occupation
, that takes in two arguments––the data hash and a string of a hometown. It returns the occupation of the first contestant who hails from that hometown. - Build a method,
get_average_age_for_season
, that takes in two arguments––the data hash and a string of a season. Iterate through the hash and return the average age of all of the contestants for that season.
- How will you iterate down into the level of the hash that contains each contestant's age?
- How will you collect the ages of each contestant and average them? Remember that the values of the "age" keys are not numbers, they are strings. How do we convert strings to numbers in Ruby?
- Remember that dividing integers in Ruby always rounds down. In this case though, we want the normal math sort of rounding (where .5 and higher rounds up), instead. Consider the difference between
to_f
andto_i
.
- Ruby's each method on arrays
- Ruby's each method on hashes
- Ruby's hsh[key] → value method
- JSON Data
- Rounding in Ruby
View Advanced Nested Hash Manipulation II: The Bachelor on Learn.co and start learning to code for free.