Using JSON in R

First tried RJSONIO with an online tutorial,”Using R to download and parse JSON: an example using data from an open data portal“, but kept getting a connection error…

> library(RJSONIO)
> foodMarketsRaw<-fromJSON("https://data.ny.gov/api/views/9a8c-vfzj/rows.json?accessType=DOWNLOAD") Error in file(con, "r") : cannot open the connection > 
> foodMarketsRaw<-fromJSON("https://data.ny.gov/api/views/9a8c-vfzj/rows.json?accessType=DOWNLOAD") Error in file(con, "r") : cannot open the connection > foodMarketsRaw<-fromJSON("retail_food_markets.json")
Error in file(con, "r") : cannot open the connection

… so I tried jsonlite.

First I tried to install jsonlite with …

> install.packages("jsonlite")

… which worked, but when i tried to use it i got a curl error….

> library(jsonlite)
> hadley_orgs <- fromJSON("https://api.github.com/users/hadley/orgs")
Error: Required package curl not found. Please run: install.packages('curl')

… which is probably why my initial RJSONIO attempted failed. So next I try to install curl, but get a compilation error….

> install.packages('curl')
File curl.h not found. Make sure the curl development library is installed, e.g. libcurl4-openssl-dev (deb) or libcurl-devel (rpm).
ERROR: configuration failed for package ‘curl’

Looks like I need the curl development package installed on my linux os. I have CentOS so search for the libcurl packages and install them with….

# yum list libcurl*
# yum install libcurl-devel

Now I try to run my jsonlite example again and it works like a charm….

> library(curl)
> library(jsonlite)
> hadley_orgs <- fromJSON("https://api.github.com/users/hadley/orgs")
> hadley_orgs

 

Fetching JSON data from REST APIs

Using R to download and parse JSON: an example using data from an open data portal