0

I'm using jsonr to read a JSON file in to R. However, the fromJSON(file="file.json") command is only reading the first line of the file. Here's the JSON:

{"id":"a","emailAddress":"a@a.com","name":"abc"}
{"id":"b","emailAddress":"b@b.com","name":"def"}
{"id":"c","emailAddress":"c@c.com","name":"ghi"}

How do I get all 3 rows into an R dataframe? Note that the above content lives in a single file.

Logica
  • 977
  • 4
  • 16
Moon_Watcher
  • 416
  • 1
  • 6
  • 19

1 Answers1

2

I found a hacky way to do that; First i read in the whole file/string with readr, then i split the data by new lines "\n", and finally i parse each line with fromJSON and then i bind it into one dataframe:

library(jsonlite)
library(readr)

json_raw   <- readr::read_file("file.json")
json_lines <- unlist(strsplit(json_raw, "\\n"))
json_df    <- do.call(rbind, lapply(json_lines, 
                                  FUN = function(x){as.data.frame(jsonlite::fromJSON(x))}))
SebSta
  • 476
  • 2
  • 12