jsonlite
jsonlite copied to clipboard
`stream_in` doesn't process jsonlines examples
On the jsonlines website, the following example is given (represented in R):
json <- "[\"Name\", \"Session\", \"Score\", \"Completed\"]
[\"Gilbert\", \"2013\", 24, true]
[\"Alexa\", \"2013\", 29, true]
[\"May\", \"2012B\", 14, false]
[\"Deloise\", \"2012A\", 19, true]"
The best way to stream this data in would be:
xx <- textConnection(json, open="r")
headers <- jsonlite::fromJSON(readLines(xx,1))
data <- jsonlite::stream_in(xx)
close(xx)
names(data) <- headers
However, if one looks at the structure of the data:
> str(data)
'data.frame': 4 obs. of 4 variables:
$ Name : Factor w/ 4 levels "Alexa","Deloise",..: 3 1 4 2
$ Session : Factor w/ 3 levels "2012A","2012B",..: 3 3 2 1
$ Score : Factor w/ 4 levels "14","19","24",..: 3 4 1 2
$ Completed: Factor w/ 2 levels "FALSE","TRUE": 2 2 1 2
Given that this is one of the examples given by the jsonlines site, it should be parsed into a data.frame with the correct types, shouldn't it?
Mmm good point. In practice all examples of ndjson/jsonlites data that I have seen use top-level json objects (as in the other examples). But it would be nice to get this structure to work as well...