将JSON文件转换为R中的data.frame

时间:2017-02-08 17:03:35

标签: json r dataframe import

我有一个文件,其中包含JSON格式的事件数据(点击用户与app交互的数据),我需要在数据框中转换它。我用过这个:

require(RJSONIO)
result <- fromJSON('events_data.json',nullValue = NA)
result <- do.call(rbind,lapply(result,data.frame,stringsAsFactors=FALSE))

生成此结果

summary(result)
                  Length Class  Mode     
_id               1      -none- character
session           2      -none- list     
metrics           0      -none- list     
arrival_timestamp 1      -none- character
event_type        1      -none- character
event_timestamp   1      -none- character
event_version     1      -none- character
application       7      -none- list     
client            2      -none- character
device            4      -none- list     
attributes        3      -none- character

当我尝试将此列表列表转换为数据框时,我收到错误

Error in data.frame(locale = c("US", "en_US", "en"), platform = c("5.1.1",  : 
  arguments imply differing number of rows: 3, 2, 1

这是数据文件click here(仅供参考)。有人可以帮我吗。 JSON文件包含

{"_id":{"$oid":"57a30ce268fd0809ec4d194f"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"OfferViewed","event_timestamp":{"$numberLong":"1470183505399"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"Category":"120000","CustomerID":"4078","OfferID":"45436"}}
{"_id":{"$oid":"57a30ce268fd0809ec4d1950"},"session":{"start_timestamp":{"$numberLong":"1470183490481"},"session_id":"def5faa9-20160803-001810481"},"metrics":{},"arrival_timestamp":{"$numberLong":"1470183523054"},"event_type":"ContextMenuItemSelected","event_timestamp":{"$numberLong":"1470183500206"},"event_version":"3.0","application":{"package_name":"com.think.vito","title":"Vito","version_code":"5","app_id":"7ffa58dab3c646cea642e961ff8a8070","cognito_identity_pool_id":"us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74","version_name":"2.0.0.0","sdk":{"version":"2.2.2","name":"aws-sdk-android"}},"client":{"cognito_id":"us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37","client_id":"ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"},"device":{"locale":{"country":"US","code":"en_US","language":"en"},"platform":{"version":"5.1.1","name":"ANDROID"},"make":"YU","model":"AO5510"},"attributes":{"MenuItem":"OfferList","CustomerID":"4078"}}

2 个答案:

答案 0 :(得分:0)

我能够很好地下载和读取您的数据。使用onTouched()库。看一下这个。如果它能为您提供您正在寻找的东西,请告诉我......

ndjson

祝你好运。 干杯, NF

答案 1 :(得分:0)

我得到了答案,实际上我并没有阅读整个json文件。

library(rjson)
library(plyr)

json_file <- "events_data.json"

con <- file(json_file, "r")
input <- readLines(con,-1L)
close(con)

json_file2 <- ldply(lapply(input, function(x) t(unlist(fromJSON(x)))))