Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import multiple json files from a directory and attaching the data

I am trying to read multiple json files into a working directory for further converting into a dataset. I have files text1, text2, text3 in the directory json. Here is the code i wrote:

setwd("Users/Desktop/json")
temp = list.files(pattern="text*.")
myfiles = lapply(temp, read.delim)
library("rjson")
json_file <- "myfiles"
library(jsonlite)
out <- jsonlite::fromJSON(json_file)
out[vapply(out, is.null, logical(1))] <- "none"
data.frame(out, stringsAsFactors = FALSE)[,1:5]
View(out)

I have about 200 files so i was wondering if there is way in which the json files can be imported.

Thanks

like image 990
user3570187 Avatar asked Nov 14 '14 20:11

user3570187


1 Answers

I think that I had a similar problem when working w/ Twitter data. I had a directory containing separate files for each user name, and I wanted to import/analyze them as a group. This worked for me:

library(rjson)
filenames <- list.files("Users/Desktop/json", pattern="*.json", full.names=TRUE) # this should give you a character vector, with each file name represented by an entry
myJSON <- lapply(filenames, function(x) fromJSON(file=x)) # a list in which each element is one of your original JSON files

If this doesn't work, then I need a bit more information to understand your problem.

like image 180
user2047457 Avatar answered Nov 01 '22 17:11

user2047457