Reputation: 9423
I tried to save JSON string to CSV with fwrite
. But I can't load it back with fread
.
Question: What the quotes settings should I use here?
library(data.table)
library(jsonlite)
r <- list(a = "text")
d <- data.table(
a = toJSON(r, auto_unbox = TRUE)
)
tmp <- tempfile()
fwrite(d, tmp, quote = TRUE, qmethod = "escape")
fromJSON(fread(tmp)[["a"]][1])
Code above produce error:
Error: lexical error: invalid char in json text.
{\"a\":\"text\"}
(right here) ------^
Upvotes: 5
Views: 885
Reputation: 9423
Solution with qmethod = "double"
for the current version (1.12.3):
library(data.table)
library(jsonlite)
r <- list(a = "text")
d <- data.table(
a = toJSON(r, auto_unbox = TRUE)
)
tmp <- tempfile()
fwrite(d, tmp, quote = TRUE, qmethod = "double")
fromJSON(gsub("\"\"", "\"", fread(tmp)[["a"]], fixed = TRUE)[1])
Upvotes: 4
Reputation: 11898
It seems you can't.
The docs for fread()
clearly state that slash-escaped quotes should be handled in quoted columns:
Unescaped quotes may be present in a quoted field, e.g., ...,2,"Joe, "Bloggs"",3.14,..., as well as escaped quotes, e.g., ...,2,"Joe \",Bloggs\"",3.14,....
If this were the case, the method you are using should work. In fact, a search on the data.table GitHub repository reveals an open issue about this. In the mean time, you'll need to either fix the data after fread()
, or use some other function to read the data.
Upvotes: 2