Demonstration

To Follow Along

OR

  1. Open the Notebook in an RStudio Docker Container - https://vm-manage.oit.duke.edu/containers/rstudio
  2. File > New Project…
  3. Version Control > Git
  4. Repository URL = https://github.com/libjohn/r-api-json.git > Create Project
  5. Open demonstration.Rmd file
    • Run All

Overview of OMDB api


Demonstration

# https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html
library(jsonlite)

Single JSON object

When the API response is a single JSON object, JSONlite puts the response into a single item list which allows for a simple view the data.

oneJSONresult <- fromJSON("http://www.omdbapi.com/?t=rocky&y=&plot=full&r=json")

oneJSONresult

The elements of the list behave as you would expect in R.
  • List all the variable names.
names(oneJSONresult)
  • List an individual element
oneJSONresult$Title
oneJSONresult$Awards

Multiple objects in API response

Note: The results of this code-snippet react differently between the console, the Notebook script (console), and the Notebook HTML output. In the script-output you can find the object names of the list, in this case dollar-search: $Search. Or, you can use bracket notation: [[1]]. Once you identify the object name, it’s easier to identify the variable names within the $Search data frame.

Below, the structure of the API response for a series search is to have a data.frame with the search results ($Search), and two other list components ($TotalResults, $Response). We’re interested in the results, the $Search data.frame.

jsonSeriesResultsList <- fromJSON("http://www.omdbapi.com/?s=rocky&type=series&r=json&page=1")
jsonSeriesResultsList

Search results of the JSON list as a data frame.

jsonSeriesResultsList$Search

Title vector of the Search Data Frame

jsonSeriesResultsList$Search$Title

LS0tDQp0aXRsZTogIlVzaW5nIFIgdG8gT3JjaGVzdHJhdGUgQVBJcyAtLSBEZW1vbnN0cmF0aW9uIg0KYXV0aG9yOiAiSm9obiBMaXR0bGUiDQpkYXRlOiAnYHIgU3lzLkRhdGUoKWAnDQpvdXRwdXQ6DQogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQNCi0tLQ0KDQojIyBEZW1vbnN0cmF0aW9uDQoNCiMjIyBUbyBGb2xsb3cgQWxvbmcNCg0KLSBPcGVuIHRoZSBXZWIgUGFnZSAtIGh0dHA6Ly93d3cuam9obmxpdHRsZS5pbmZvL3Byb2plY3QvY3VzdG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