0

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?
Jaap
  • 81,064
  • 34
  • 182
  • 193
Fenil Dedhia
  • 365
  • 2
  • 21
  • You're not doing anything wrong, visit `https://data.maryland.gov/api/views/pdvh-tf2u/rows.json?accessType=DOWNLOAD` and look at the JSON -- there's two top-level fields, `meta` and `data` -- in your case, look at `myjson$meta` and `myjson$data`. Also look at `names(myjson)`. – JasonAizkalns Feb 18 '16 at 20:19

2 Answers2

2

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 ...
HubertL
  • 19,246
  • 3
  • 32
  • 51
1

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)
cory
  • 6,529
  • 3
  • 21
  • 41