在R中加载和转换json文件

时间:2017-02-12 23:09:13

标签: json r matrix dataframe

我有.json文件,我需要在R中加载并在将其转换为数据帧后对其执行进一步的操作。我的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,会话,指标,并且有很多类似的。

我尝试使用R中的rjson库进行转换,如下所示。 events_jason是文件名:

library(rjson)
result <- fromJSON(file = "events_json.json")
print(result)



 $`_id`
$`_id`$`$oid`
[1] "57a30ce268fd0809ec4d194f"


$session
$session$start_timestamp
$session$start_timestamp$`$numberLong`
[1] "1470183490481"


$session$session_id
[1] "def5faa9-20160803-001810481"


$metrics
list()

$arrival_timestamp
$arrival_timestamp$`$numberLong`
[1] "1470183523054"


$event_type
[1] "OfferViewed"

$event_timestamp
$event_timestamp$`$numberLong`
[1] "1470183505399"


$event_version
[1] "3.0"

$application
$application$package_name
[1] "com.think.vito"

$application$title
[1] "Vito"

$application$version_code
[1] "5"

$application$app_id
[1] "7ffa58dab3c646cea642e961ff8a8070"

$application$cognito_identity_pool_id
[1] "us-east-1:4d9cf803-0487-44ec-be27-1e160d15df74"

$application$version_name
[1] "2.0.0.0"

$application$sdk
$application$sdk$version
[1] "2.2.2"

$application$sdk$name
[1] "aws-sdk-android"



$client
$client$cognito_id
[1] "us-east-1:2e26918b-f7b1-471e-9df4-b931509f7d37"

$client$client_id
[1] "ee0b61b0-85cf-4b2f-960e-e2aedef5faa9"


$device
$device$locale
$device$locale$country
[1] "US"

$device$locale$code
[1] "en_US"

$device$locale$language
[1] "en"


$device$platform
$device$platform$version
[1] "5.1.1"

$device$platform$name
[1] "ANDROID"


$device$make
[1] "YU"

$device$model
[1] "AO5510"


$attributes
$attributes$Category
[1] "120000"

$attributes$CustomerID
[1] "4078"

$attributes$OfferID
[1] "45436"

但它正如我上面提到的那样显示/读取第一行。还有其他更多的ID,会话,指标,event_type等未显示。

请帮助我如何阅读我的整个json文件,以便我可以看到其他行并将其转换为正确的数据框。

更新

我找到了解决方案。使用ndjson包我得到了理想的数据框。

library(ndjson)
df<-ndjson::stream_in('events_data.json')

1 个答案:

答案 0 :(得分:0)

您的文件不是单个json对象,而是一个json obejcts列表,每行一个。你必须阅读每一行并从json转换每一行 一种方法是:

d <- lapply(strsplit(readLines("events_data2.json"),"\n"), fromJSON)

希望这有帮助