# install.packages('jsonlite')
library(jsonlite)
# R originally treats JSON as a string of characters
<- '{"name": "John", "age": 30, "city": "New York", "hobbies": ["reading", "hiking", "coding"]}' json
13 APIs - Part 1
Settling In
Quiz 2 Revisions
On a separate piece of paper, please complete the following:
- For every question I marked with an X on the Quiz 2,
- write a more correct answer
- 1 sentence describing how your understanding has changed since the quiz
Turn in this piece of paper + the original quiz next Monday in class.
Data Storytelling Moment
Quick Review
map(list_or_vector, function)
: iterate over elements in list or vector and pass to function as the 1 and only input
pmap(list_of_vectors, function)
: iterate over elements in (best if named) list of vectors and pass each to function as an named input
walk(list_or_vector, function)
: same as map()
but without saving R output from the iteration
pwalk(list_of_vectors, function)
: same as pmap()
but without saving R output from the iteration
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:
{
"name": "John",
"age": 30,
"city": "New York",
"hobbies": ["reading", "hiking", "coding"]
}
. . .
JSON Syntax:
- Curly Braces
{}
: These 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
[]
: These enclose an array, which is an ordered list of values. - Double Quotes: the key and any string value must be in double quotes
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.
. . .
#fromJSON() converts JSON characters to R objects
<- fromJSON(json)
data_in_r
data_in_r
$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).
%>% class() data_in_r
[1] "list"
. . .
If we have an array of objects, we can imagine each element of the array is one row of a dataset.
<-
json '[
{"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"},
{"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"},
{},
{"Name" : "Bowser", "Occupation" : "Koopa"}
]'
. . .
# If we don't try to simplify, fromJSON() provides a list
fromJSON(json, simplifyVector = FALSE)
[[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"
. . .
# If it is an array that satisfies the constraints of a data frame, it may be able to simplify to data frame
fromJSON(json, simplifyVector = TRUE)
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
Challenge work
Go to Homework 7 and work on Part 1 with people in class. You’ll want to review material from the last class and map()
functions.
After Class
- Take a look at the Schedule page to see how to prepare for the next class.
- Work on Homework 7.