Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文件中的json条目转换为数据帧?_Json_R - Fatal编程技术网

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

如何将文件中的json条目转换为数据帧?,json,r,Json,R,我试图读取包含json内容的文件,并根据一些字段将其转换为表格数据 该文件包含以下内容: {"senderDateTimeStamp":"2016/04/08 10:03:18","senderHost":null,"senderCode":"web_app","senderUsecase":"appinternalstats_prod","destinationTopic":"web_app_appinternalstats_realtimedata_topic","correlatedRec

我试图读取包含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"}
我需要能够转换时间戳,源,主机,状态字段和每行的payloadData部分到R中的数据帧

我试过这个:

图书馆(rjson)
这可能是你想要的:

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)}))
库(rjson)
d考虑一下包装:

library(tidyjson)
图书馆(magrittr)
json%
收集密钥()
#上头
#document.id密钥
#1 1发送日期时间戳
#2.1发送者主机
#3.1发送代码
#4.1仙人掌糖酶
#5.1目的地
#6.1相关记录
json%>%
输入对象(“payloadData”)%>%
聚集_键()%>%
附加\u值\u字符串()
#上头
#document.id键字符串
#1时间戳2016-04-08T10:03:18.244
#2.1状态获取
#3.1源MSG1
#4 1项目测试1
#5.1基础
#6.1 pricingdate

运行代码时会出现错误:结构错误(list(timestamp=“2016-04-08T10:03:18.244”,status=“get”,:“names”属性[16]必须与向量[15]长度相同@user1357015,我已使用工作dput输出更新帖子,我在以下位置获取此错误:错误:意外“}”:“Source=d$payloadData$Source,Status=d$payloadData$Status}”>)错误:意外“')”在“>”中)错误:意外“')”“d$payloadData:$operator中的错误对于原子向量无效您是只想转换一个d列表还是文件中有多个d?”?如果是前一种情况,那么
myDf的
d
结构是什么样的?它是您在代码中给出的结构列表吗?如果是,那么修改后的代码应该适合您。然后您可能想尝试以下操作:
data@JasonAiskalns,json数据在一个文件中。我首先这样做是为了将它读入一个对象:数据
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)}))
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