Importing JSON data in R to be saved as dataframe

727 Views Asked by At

I am trying to import JSON data from a URL and save it as a dataframe. Once I have it in a single rows/columns format dataframe, I want to perform cleaning operations like removing some values and columns.

I am using jsonlite package to automatically parse the data and save in dataframe format. However, it seems to create a list of dataframes instead of one dataframe.

#install
install.packages("jsonlite")

#load
library(jsonlite)

#fetch JSON data
litejson <- "https://data.maryland.gov/api/views/pdvh-tf2u/rows.json?accessType=DOWNLOAD"
myjson <- fromJSON(litejson)
print(myjson)

# It seems the jsonlite didn't parse the data properly. It is a list of data frames instead of one single data frame.
str(myjson)

#WHAT AM I DOING WRONG?
2

There are 2 best solutions below

0
On BEST ANSWER

Just download it as a csv instead. It saves a lot of conversion code...

df <- read.csv("https://data.maryland.gov/api/views/pdvh-tf2u/rows.csv", stringsAsFactors=FALSE)
0
On

To get a data.frame, just do

df <- data.frame(myjson$data)
str(df)

'data.frame':   18638 obs. of  26 variables:
 $ X1 : Factor w/ 18638 levels "1","10","100",..: 1 9751 10862 11973 13084 14195 15306 16417 17528 2 ...
 $ X2 : Factor w/ 18638 levels "0006A909-5D07-4AFF-A282-439A725518E0",..: 18379 15224 7798 6286 6035 13134 12495 3981 4781 8051 ...