如何在R中解析具有堆叠的多个JSON的文件? [英] How to parse a file with stacked multiple JSONs in R?
问题描述
我在R example1.json
中具有以下堆叠的JSON"对象:
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"},…]}
这些不是逗号分隔的.基本目标是将某些字段(或所有字段)解析为R data.frame或data.table:
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
通常,我会在R中读取JSON,如下所示:
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
此文件格式称为换行分隔的JSON",即NDJSON.
This file format is called a "newline delimited JSON", NDJSON.
推荐答案
-
三个点
...
使您的JSON无效,因此使您的lexical error
无效.
The three dots
...
invalidate your JSON, hence yourlexical error
.
您可以使用jsonlite::stream_in()
在JSON行中流式传输".
You can use jsonlite::stream_in()
to 'stream in' lines of JSON.
library(jsonlite)
jsonlite::stream_in(file("~/Desktop/examples1.json"))
# opening file input connection.
# Imported 3 records. Simplifying...
# closing file input connection.
# ID Timestamp Usefulness Code
# 1 12345 20140101 Yes A, 1
# 2 1A35B 20140102 No B, 1
# 3 AA356 20140103 No B, 0
数据
我已经清理了示例数据以使其成为有效的JSON,并将其另存为~/Desktop/examples1.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"}]}
这篇关于如何在R中解析具有堆叠的多个JSON的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!