Reputation: 71
For my PhD project I need to read a JSONL file into R (the extension isn't json, is jsonl) and transform it in a csv. I tried to use this code based on jsonlite but it gives me an error:
library(jsonlite)
data <- "/Users/chiarap/tweets.jsonl"
dat <- fromJSON(sprintf("[%s]", paste(readLines(data), collapse=",")))
Error: parse error: unallowed token at this point in JSON text
EFEF","notifications":null}},,,{"in_reply_to_status_id_str":
(right here) ------^
Thanks
Upvotes: 7
Views: 4078
Reputation: 81
A much more straightforward approach makes use of jsonlite's built in function stream_in()
. Requires no applys, unlisting or other manipulation.
library(jsonlite)
jsonl_file <- "my_file.jsonl"
df <- stream_in(file(jsonl_file))
Found here: https://forum.posit.co/t/error-when-reading-the-first-line-in-jsonl-file-in-r/135498
Upvotes: 2
Reputation: 576
If you have a large file, pasting all of the rows together may result in errors. You can process each line separately and then combine them into a data frame.
library(jsonlite)
library(dplyr)
lines <- readLines("myfile.jsonl")
lines <- lapply(lines, fromJSON)
lines <- lapply(lines, unlist)
x <- bind_rows(lines)
Then x is a data frame that you can continue to work with or write to file.
Upvotes: 11