如何将文件中的json条目转换为数据框?

时间:2016-04-13 17:21:37

标签: json r

我正在尝试读取具有json内容的文件,并根据某些字段将其转换为表格数据。

该文件包含以下内容:

DefaultConnection

我需要能够将每行的payloadData部分的时间戳,源,主机,状态字段转换为R中的数据帧。

我试过这个:

库(rjson) d< -fromJSON(文件= “file.txt的”)

{"senderDateTimeStamp":"2016/04/08 10:03:18","senderHost":null,"senderCode":"web_app","senderUsecase":"appinternalstats_prod","destinationTopic":"web_app_appinternalstats_realtimedata_topic","correlatedRecord":false,"needCorrelationCacheCleanup":false,"needCorrelation":false,"correlationAttributes":null,"correlationRecordCount":0,"correlateTimeWindowInMills":0,"lastCorrelationRecord":false,"realtimeESStorage":true,"receiverDateTimeStamp":1460124283554,"payloadData":{"timestamp":"2016-04-08T10:03:18.244","status":"get","source":"MSG1","ITEM":"TEST1","basis":"","pricingdate":"","content":"","msgname":"","idlreqno":"","host":"web01","Webservermember":"Web"},"payloadDataText":"","key":"web_app:appinternalstats_prod","destinationTopicName":"web_app_appinternalstats_realtimedata_topic","esindex":"web_app","estype":"appinternalstats_prod","useCase":"appinternalstats_prod","Code":"web_app"}

如何将json条目的payloadData部分转换为数据框?

2 个答案:

答案 0 :(得分:1)

这可能是你想要的东西:

library(rjson)
d<-fromJSON(file="file.txt")
myDf <- do.call("rbind", lapply(d, function(x) { 
               data.frame(TimeStamp = x$payloadData$timestamp, 
                          Source = x$payloadData$source, 
                          Host = $payloadData$host, 
                          Status = x$payloadData$status)}))

答案 1 :(得分:1)

考虑包tidyjson

library(tidyjson)
library(magrittr)

json <- '{"senderDateTimeStamp":"2016/04/08 10:03:18","senderHost":null,"senderCode":"web_app","senderUsecase":"appinternalstats_prod","destinationTopic":"web_app_appinternalstats_realtimedata_topic","correlatedRecord":false,"needCorrelationCacheCleanup":false,"needCorrelation":false,"correlationAttributes":null,"correlationRecordCount":0,"correlateTimeWindowInMills":0,"lastCorrelationRecord":false,"realtimeESStorage":true,"receiverDateTimeStamp":1460124283554,"payloadData":{"timestamp":"2016-04-08T10:03:18.244","status":"get","source":"MSG1","ITEM":"TEST1","basis":"","pricingdate":"","content":"","msgname":"","idlreqno":"","host":"web01","Webservermember":"Web"},"payloadDataText":"","key":"web_app:appinternalstats_prod","destinationTopicName":"web_app_appinternalstats_realtimedata_topic","esindex":"web_app","estype":"appinternalstats_prod","useCase":"appinternalstats_prod","Code":"web_app"}'

json %>%
  gather_keys()

# head() of above
#   document.id                 key
# 1           1 senderDateTimeStamp
# 2           1          senderHost
# 3           1          senderCode
# 4           1       senderUsecase
# 5           1    destinationTopic
# 6           1    correlatedRecord

json %>%
  enter_object("payloadData") %>%
  gather_keys() %>%
  append_values_string()

# head() of above
#   document.id         key                  string
# 1           1   timestamp 2016-04-08T10:03:18.244
# 2           1      status                     get
# 3           1      source                    MSG1
# 4           1        ITEM                   TEST1
# 5           1       basis                        
# 6           1 pricingdate