Reading JSON data

Several RESTful web services return data in JSON format—in some ways simpler and more efficient than XML. This recipe shows you how to read JSON data.

Getting ready

R provides several packages to read JSON data, but we use the jsonlite package. Install the package in your R environment as follows:

> install.packages("jsonlite")

If you have not already downloaded the files for this chapter, do it now and ensure that the students.json files and student-courses.json files are in your R working directory.

How to do it...

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

  1. Load the library:
    > library(jsonlite)
  2. Load the JSON data from files:
    > dat.1 <- fromJSON("students.json") > dat.2 <- fromJSON("student-courses.json") ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.