13 APIs P1
🚩 Pre-Class Learnings
To prepare for this lesson, do the followings:
- Read A Non-Programmer’s Introduction to JSON by Scott Lowe
- Read Understanding URL by Tutorials Point
- Read
urltools
package vignette Elegant URL handling with urltools
🔥 Data Story Critique
Go to https://www.nytimes.com/interactive/2024/09/18/business/economy/mortgages-car-loans-interest-rates.html then answer the following questions:
- What is the data story?
- What is effective?
- What could be improved?
JSON files
JSON (JavaScript Object Notation) is a lightweight, human-readable data-interchange format.
- It is easy for humans to read and write.
- It is easy for machines to parse and generate.
- It is commonly used for APIs (Application Program Interface) to share data.
Here is an example of the most basic format of JSON:
JSON Syntax:
- Curly braces
{}
enclose an object, which is a collection of key-value pairs. - Key-value pairs–each pair consists of a key (string) and a value (can be string, number, boolean, array, or another object).
- Colon
:
separates the key from the value. - Comma
,
separates different key-value pairs within the object. - Square brackets
[]
enclose an array, which is an ordered list of values. - Double quotes used to surround the key and any string value
Reading JSON Files
There are a few packages we could use to read JSON files into R.
We will start by using the jsonlite
package today.
$name
[1] "John"
$age
[1] 30
$city
[1] "New York"
$hobbies
[1] "reading" "hiking" "coding"
What type of object is this?
A named list (but elements are not all of the same length).
If we have an array of objects, we can imagine each element of the array is one row of a dataset.
[[1]]
[[1]]$Name
[1] "Mario"
[[1]]$Age
[1] 32
[[1]]$Occupation
[1] "Plumber"
[[2]]
[[2]]$Name
[1] "Peach"
[[2]]$Age
[1] 21
[[2]]$Occupation
[1] "Princess"
[[3]]
named list()
[[4]]
[[4]]$Name
[1] "Bowser"
[[4]]$Occupation
[1] "Koopa"
Name Age Occupation
1 Mario 32 Plumber
2 Peach 21 Princess
3 <NA> NA <NA>
4 Bowser NA Koopa
For more info about converting JSON data to R objects with jsonlite, see https://cran.r-project.org/web/packages/jsonlite/vignettes/json-mapping.pdf