OR
# https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html
library(jsonlite)
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"
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"
oneJSONresult$Title
[1] "Rocky"
oneJSONresult$Awards
[1] "Won 3 Oscars. Another 16 wins & 21 nominations."
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"
jsonSeriesResultsList$Search
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"