specify the path to the common folder with json files in R

702 Views Asked by At

to parse json, i can use this approach

 library("rjson")
    json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"
    json_data <- fromJSON(paste(readLines(json_file), collapse=""))

but what if i want work with set of json files it located

json_file<-"C:/myfolder/"

How to parse in to data.frame all json files in this folder? (there 1000 files)?

1

There are 1 best solutions below

0
On BEST ANSWER

A lot of missing info, but this will probably work.. I used pblapply to get a nice progress-bar (since you are mentioning >1000 files).

I never used the solution below for JSON-files (no experience wit JSON), but it works flawless on .csv and .xls files ( of course with different read-functions).. so I expect it to work with JSON also.

library(data.table)
library(pbapply)
library(rjson)

folderpath <- "C:\\myfolder\\"
filefilter <- "*.json$"

#set paramaters as needed
f <- list.files( path = folderpath,
                 pattern = filefilter,
                 full.names = TRUE,
                 recursive = FALSE )

#read all files to a list
f.list <- pblapply( f, function(x) fromJSON( file = x ) )

#join lists together
dt <- data.table::rbindlist( f.list )