13 APIs P1

🚩 Pre-Class Learnings

To prepare for this lesson, do the followings:

🔥 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:

{
  "name": "John",
  "age": 30,
  "city": "New York",
  "hobbies": ["reading", "hiking", "coding"]
}

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.

# install.packages('jsonlite')
library(jsonlite)

# R originally treats JSON as a string of characters
json <- '{"name": "John", "age": 30, "city": "New York", "hobbies": ["reading", "hiking", "coding"]}'
#fromJSON() converts JSON characters to R objects
data_in_r <- fromJSON(json)

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).

data_in_r |> class()
[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