使用tidyjson将R JSON文件转换为Dataframe

时间:2017-03-06 18:14:15

标签: json r

我使用以下格式从Mongodb导出了一个JSON文件。我正在尝试从它创建一个数据帧,但我看不到让tidyjson读取它,因为它会抛出此错误。

Error: lexical error: invalid char in json text.
                        {      "_id" : ObjectId("586e684427a06a4a658fa
                     (right here) ------^

我使用了read_json(“file.json”)

文件在

下面
{ 
    "_id" : ObjectId("586e684427a06a4a658fa28e"), 
    "expires_in" : ISODate("2016-11-19T22:16:57.418+0000"), 
    "job_type" : "Satellite Sales & Service", 
    "inbound_id" : ObjectId("586e68440c83945fb2658754"), 
    "created_at" : ISODate("2017-01-05T15:37:40.850+0000"), 
    "action_states" : [
        {
            "_id" : ObjectId("586e684627a06a4a658fa293"), 
            "transition_duration" : NumberInt(0), 
            "name" : Symbol("created"), 
            "actor" : "home_owner", 
            "created_at" : ISODate("2017-01-05T15:37:42.297+0000")
        }, 
        {
            "_id" : ObjectId("586e68ad0c83945fb2658825"), 
            "transition_duration" : NumberInt(1), 
            "name" : Symbol("accepted"), 
            "reason" : null, 
            "actor" : "contractor", 
            "created_at" : ISODate("2017-01-05T15:39:25.924+0000")
        }
    ]
}
{ 
    "_id" : ObjectId("586e675d27a06a4a658fa264"), 
    "expires_in" : ISODate("2016-11-19T22:16:57.418+0000"), 
    "job_type" : "Satellite Sales & Service", 
    "inbound_id" : ObjectId("586e675d0c83945fa2f6e190"), 
    "created_at" : ISODate("2017-01-05T15:33:49.934+0000"), 
    "action_states" : [
        {
            "_id" : ObjectId("586e675f27a06a4a658fa267"), 
            "transition_duration" : NumberInt(0), 
            "name" : Symbol("created"), 
            "actor" : "home_owner", 
            "created_at" : ISODate("2017-01-05T15:33:51.097+0000")
        }, 
        {
            "_id" : ObjectId("586e694c0c83945faae36559"), 
            "transition_duration" : NumberInt(8), 
            "name" : Symbol("accepted"), 
            "reason" : null, 
            "actor" : "contractor", 
            "created_at" : ISODate("2017-01-05T15:42:04.116+0000")
        }
    ]
}

1 个答案:

答案 0 :(得分:3)

由于mnogodb产生的这个错误导致"扩展" json格式。您应该尝试将数据导出为严格的' json模式使用,例如,mongoexport(https://docs.mongodb.com/manual/reference/mongodb-extended-json/