I am struggling to parse a JSON in R which contains newlines both within character strings and between key/value pairs (and whole objects).
Here's the sort of format I mean:
{
"id": 123456,
"name": "Try to parse this",
"description": "Thought reading a JSON was easy? \r\n Try parsing a newline within a string."
}
{
"id": 987654,
"name": "Have another go",
"description": "Another two line description... \r\n With 2 lines."
}
Say that I have this JSON saved as example.json
. I have tried various techniques to overcome parsing problems, suggested elsewhere on SO. None of the following works:
library(jsonlite)
foo <- readLines("example.json")
foo <- paste(readLines("example.json"), collapse = "")
bar <- fromJSON(foo)
bar <- jsonlite::stream_in(textConnection(foo))
bar <- purrr::map(foo, jsonlite::fromJSON)
bar <- ndjson::stream_in(textConnection(foo))
bar <- read_json(textConnection(foo), format = "jsonl")
I gather that this is really NDJSON format, but none of the specialised packages cope with it. Some suggest streaming in the data with either jsonlite or ndjson (or this one and this one). Others suggest mapping the function across lines (or similarly in base R).
Everything raises one of the following errors:
Error: parse error: trailing garbage
or Error: parse error: premature EOF
or problems opening the text connection.
Does anyone have a solution?