R Data Analysis Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

Once the files are ready, load the jsonlite package and read the files as follows:

  1. Load the library:
> library(jsonlite) 
  1. Load the JSON data from the files:
> dat.1 <- fromJSON("students.json") 
> dat.2 <- fromJSON("student-courses.json")
  1. Load the JSON document from the web:
> url <- "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json" 
> jsonDoc <- fromJSON(url)
  1. Extract the data into data frames:
> dat <- jsonDoc$list$resources$resource$fields 
> dat.1 <- jsonDoc$list$resources$resource$fields
> dat.2 <- jsonDoc$list$resources$resource$fields
  1. Verify the results:
> dat[1:2,] 
> dat.1[1:3,]
> dat.2[,c(1,2,4:5)]