I have the following "stacked JSON" object within R, example1.json
:
{"ID":"12345","Timestamp":"20140101", "Usefulness":"Yes",
"Code":[{"event1":"A","result":"1"},…]}
{"ID":"1A35B","Timestamp":"20140102", "Usefulness":"No",
"Code":[{"event1":"B","result":"1"},…]}
{"ID":"AA356","Timestamp":"20140103", "Usefulness":"No",
"Code":[{"event1":"B","result":"0"},…]}
These are not comma-separated. The fundamental goal would be to parse certain fields (or all fields) into an R data.frame or data.table:
Timestamp Usefulness
0 20140101 Yes
1 20140102 No
2 20140103 No
Normally, I would read in a JSON within R as follows:
library(jsonlite)
jsonfile = "example1.json"
foobar = fromJSON(jsonfile)
This however throws a parsing error:
Error: lexical error: invalid char in json text.
[{"event1":"A","result":"1"},…]} {"ID":"1A35B","Timestamp"
(right here) ------^
This is a similar question to the following, but in R: multiple Json objects in one file extract by python
EDIT: This file format is called a "newline delimited JSON", NDJSON.