How to import a JSON file into R?

r

#1

Hi all,

I wanted to know how can we import a json file into R into a dataframe. Any specific packages or functions?

Thanks


#2

Here’s an example of reading JSON I got from the Getting and Cleaning Data course on Coursera.

Reading data from JSON

Install and load packages

install.packages(“jsonlite”)
library(jsonlite)
install.packages(“curl”)
library(curl)

Read Data from URL

jsonData <- fromJSON(“https://api.github.com/users/jtleek/repos”)

Check names

names(jsonData)

Nested Objects in JSON

names(jsonData$owner)
jsonData$owner$login

Writing data frames to JSON

myjson <- toJSON(iris, pretty = TRUE)
cat(myjson)
iris2 <- fromJSON(myjson)
head(iris2)

More info on jsonlite vignette

Hope this helps!


#3

jsonlite package. another tutorial on jsonlite is here

https://www.opencpu.org/posts/jsonlite-a-smarter-json-encoder/