Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple JSON objects in fromJSON

Tags:

json

r

jsonlite

I'm trying to use fromJSON() to read in a .json file with multiple objects structured as follows:

{ "key11": value11, "key12": value12 }
{ "key11": value11, "key12": value12 }
…

If I manually add [...] brackets around the entire file, and , commas between the objects, then the following code works:

json_file <- "file.json"
json_data <- fromJSON(json_file,flatten=TRUE)

But adding the brackets and commas is not feasible for what I'm actually trying to do (I did it on a sample file).

I tried using readLines() since the objects have newlines between them, but I didn't get anywhere.

How can I read in these json objects without modifying the file?

like image 798
Plastic Soul Avatar asked Dec 30 '25 04:12

Plastic Soul


1 Answers

Assuming you know it's one per line, and no weird line breaks,

 lapply(readLines(filename), fromJSON, flatten = TRUE)
like image 190
Robert Krzyzanowski Avatar answered Jan 01 '26 16:01

Robert Krzyzanowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!