I have some 100 json files in one folder.
I want to construct a dataset in R that contains a column which contains complete data from each of the json file.
Dataset must contain a column say jsonfile
and each row in this column should contain data from one json file i.e data from 1st json file from the folder should be in first row of this column, 2nd json file data should be in second row of the column and so on. without destroying the structure of json file in this column
Can we achieve this using R? If yes, how can we do this?
I would really appreciate any help.
CodePudding user response:
In case your files are in a data folder you can iterate over it and add the info in a data frame
df <- NULL
dir <- "data/"
for (file_ in list.files(dir)){
fullPath <- paste0(dir,file_)
content <- readChar(fullPath, file.info(fullPath)$size)
df <- rbind(df, data.frame(fileName = fullPath, content = content))
}
CodePudding user response:
Without sample data and example output it's hard to know what you're asking, but to generally to bind identical json files into a single data.frame
purrr::map_dfr(
list.files("data/", full.names = TRUE), jsonlite::fromJSON
)
fromJSON
creates a data.frame from the file. map_dfr
iterates over the files and binds the data.frames together.