I'm trying to save a JSON object where each line is a record. How can I save the JSON object so that the number of lines is equal to the number of records (5 in example below)?
library(jsonlite)
df=mtcars[1:5,]
x <- jsonlite::toJSON(df)
# remove brackets
x=substr(x,2,nchar(x)-1)
write_lines(x,"tmp.json")
use jsonlite::stream_out
df <- mtcars[1:5,]
jsonlite::stream_out(df, file('tmp.json'))
that gives newline delimited JSON or "ndjson"
This might do the trick:
x2 = strsplit(x, '\\},\\{')
write.table(x2,"tmp.json")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With