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
$Title
[1] "Rocky"

$Year
[1] "1976"

$Rated
[1] "PG"

$Released
[1] "03 Dec 1976"

$Runtime
[1] "120 min"

$Genre
[1] "Drama, Sport"

$Director
[1] "John G. Avildsen"

$Writer
[1] "Sylvester Stallone"

$Actors
[1] "Sylvester Stallone, Talia Shire, Burt Young, Carl Weathers"

$Plot
[1] "Rocky Balboa is a struggling boxer trying to make the big time, working as a debt collector for a pittance. When heavyweight champion Apollo Creed visits Philadelphia, his managers want to set up an exhibition match between Creed and a struggling boxer, touting the fight as a chance for a \"nobody\" to become a \"somebody\". The match is supposed to be easily won by Creed, but someone forgot to tell Rocky, who sees this as his only shot at the big time."

$Language
[1] "English"

$Country
[1] "USA"

$Awards
[1] "Won 3 Oscars. Another 16 wins & 21 nominations."

$Poster
[1] "https://images-na.ssl-images-amazon.com/images/M/MV5BMTY5MDMzODUyOF5BMl5BanBnXkFtZTcwMTQ3NTMyNA@@._V1_SX300.jpg"

$Metascore
[1] "N/A"

$imdbRating
[1] "8.1"

$imdbVotes
[1] "387,927"

$imdbID
[1] "tt0075148"

$Type
[1] "movie"

$Response
[1] "True"

The elements of the list behave as you would expect in R.
  • List all the variable names.
names(oneJSONresult)
 [1] "Title"      "Year"       "Rated"      "Released"   "Runtime"   
 [6] "Genre"      "Director"   "Writer"     "Actors"     "Plot"      
[11] "Language"   "Country"    "Awards"     "Poster"     "Metascore" 
[16] "imdbRating" "imdbVotes"  "imdbID"     "Type"       "Response"  
  • List an individual element
oneJSONresult$Title
[1] "Rocky"
oneJSONresult$Awards
[1] "Won 3 Oscars. Another 16 wins & 21 nominations."

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

$totalResults
[1] "20"

$Response
[1] "True"

Search results of the JSON list as a data frame.

jsonSeriesResultsList$Search

Title vector of the Search Data Frame

jsonSeriesResultsList$Search$Title
 [1] "Rocky and His Friends"         "Dr. Jeff: Rocky Mountain Vet" 
 [3] "Rocky Jones, Space Ranger"     "Rocky Mountain Law"           
 [5] "Rocky King, Detective"         "Rocky Road"                   
 [7] "Rocky Mountain Bounty Hunters" "Rocky + Drago"                
 [9] "Rocky Point"                   "Rocky Star"                   

LS0tDQp0aXRsZTogIlVzaW5nIFIgdG8gT3JjaGVzdHJhdGUgQVBJcyAtLSBEZW1vbnN0cmF0aW9uIg0KYXV0aG9yOiAiSm9obiBMaXR0bGUiDQpkYXRlOiAnYHIgU3lzLkRhdGUoKWAnDQpvdXRwdXQ6DQogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQNCi0tLQ0KDQojIyBEZW1vbnN0cmF0aW9uDQoNCiMjIyBUbyBGb2xsb3cgQWxvbmcNCg0KLSBPcGVuIHRoZSBXZWIgUGFnZSAtIGh0dHA6Ly9saWJqb2huLmdpdGh1Yi5pby9yY3MyMDE3L2RlbW9uc3RyYXRpb24ubmIuaHRtbA0KDQoqKk9SKioNCg0KMS4gT3BlbiB0aGUgTm90ZWJvb2sgaW4gYW4gUlN0dWRpb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