Chiara
Chiara

Reputation: 71

How do I import a jsonl file in R and how do I transform it in csv?

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

Answers (2)

Nathan Johnson
Nathan Johnson

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

cmaimone
cmaimone

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

Related Questions