library(rjson)
filenames <- list.files(pattern="*.json") # gives a character vector, with each file name represented by an entry
Now I want to import all the JSON files into R as one single dataFrame. How do I do that?
I first tried
myJSON <- lapply(filenames, function(x) fromJSON(file=x)) # should return a list in which each element is one of the JSON files
but the above code takes along time to terminate, since I have 15,000 files, and I know it won't return a single data frame. Is there a faster way to do this?
Sample JSON file:
{"Reviews": [{"Ratings": {"Service": "4", "Cleanliness": "5"}, "AuthorLocation": "Boston", "Title": "\u201cExcellent Hotel & Location\u201d", "Author": "gowharr32", "ReviewID": "UR126946257", "Content": "We enjoyed the Best Western Pioneer Square....", "Date": "March 29, 2012"}, {"Ratings": {"Overall": "5"},"AuthorLocation": "Chicago",....},{...},....}]}
do.call(rbind,myJSON)to get a single data.frame, provided the data.frame's are compatible...purrr::map_dforpurrr::flatten_dfcould help.pbapply::pblapplywithdplyr::bind_rowswld give you a progress bar if you could get thefromJSONto give back adata.frame