【问题标题】:Problems reading JSON file in R在 R 中读取 JSON 文件时出现问题
【发布时间】:2014-03-01 07:49:12
【问题描述】:

我有一个 JSON 文件(从 mongoDB 导出),我想将它加载到 R 中。该文档大小约为 890 MB,包含 12 个字段的大约 63,000 行。这些字段是数字、字符和日期。我想最终得到一个 63000 x 12 的数据框。

lines <-  readLines("fb2013.json")

结果:jFile 包含 char 类中的所有 63,000 个元素,并且所有字段都集中在一个字段中。

每个文件看起来像这样:

"{ \"_id\" : \"10151271769737669\", \"cmets_count\" : 36, \"created_at\" : { \"$date\" : 1357941938000 }, \"icon\" : \" http://blahblah.gif\", \"likes_count\" : 450, \"link\" : \"http://www.blahblahblah.php\", \"message\" : \"我希望我能弄清楚这个!\", \"page_category\" : \"Computers\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \ "照片\", \"updated_at\" : { \"$date\" : 1358210153000 } }"

使用 rjson,

jFile <- fromJSON(paste(readLines("fb2013.json"), collapse=""))

只有第一行被读入 jFile 但有 12 个字段。

使用 RJSONIO:

jFile <- fromJSON(lines)

结果如下:

Warning messages:
1: In if (is.na(encoding)) return(0L) :
  the condition has length > 1 and only the first element will be used

同样,只有第一行被读入 jFile,共有 12 个字段。

rjson 和 RJSONIO 的输出如下所示:

$`_id`
[1] "1018535"

$comments_count
[1] 0

$created_at
       $date 
1.357027e+12 

$icon
[1] "http://blah.gif"

$likes_count
[1] 20

$link
[1] "http://www.chachacha"

$message
[1] "I'd love to figure this out."

$page_category
[1] "Internet/software"

$page_id
[1] "3924395872345878534"

$page_name
[1] "Not Entirely Hopeless"

$type
[1] "photo"

$updated_at
       $date 
1.357027e+12 

【问题讨论】:

  • 如果你想要来自 Mongo DB 的数据,为什么不使用rmongodb
  • 我发现 rmongodb 太麻烦了,RMongo 也一样。导出到 .csv 通常是我所做的并且效果很好,但在这种情况下我需要 JSON。

标签: json r rjson rjsonio


【解决方案1】:

试试

library(rjson)
path <- "WHERE/YOUR/JSON/IS/SAVED"
c <- file(path, "r")
l <- readLines(c, -1L)
json <- lapply(X=l, fromJSON)

【讨论】:

  • 非常感谢!完美运行。
  • 这救了我,尽管我不得不将 file(path, "r") 命令更改为 file(path, "rb")。
【解决方案2】:

既然你想要一个 data.frame,试试这个:

# three copies of your sample...
line.1<- "{ \"_id\" : \"10151271769737669\", \"comments_count\" : 36, \"created_at\" : { \"$date\" : 1357941938000 }, \"icon\" : \"http://blahblah.gif\", \"likes_count\" : 450, \"link\" : \"http://www.blahblahblah.php\", \"message\" : \"I wish I could figure this out!\", \"page_category\" : \"Computers\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \"photo\", \"updated_at\" : { \"$date\" : 1358210153000 } }" 
line.2<- "{ \"_id\" : \"10151271769737669\", \"comments_count\" : 36, \"created_at\" : { \"$date\" : 1357941938000 }, \"icon\" : \"http://blahblah.gif\", \"likes_count\" : 450, \"link\" : \"http://www.blahblahblah.php\", \"message\" : \"I wish I could figure this out!\", \"page_category\" : \"Computers\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \"photo\", \"updated_at\" : { \"$date\" : 1358210153000 } }" 
line.3<- "{ \"_id\" : \"10151271769737669\", \"comments_count\" : 36, \"created_at\" : { \"$date\" : 1357941938000 }, \"icon\" : \"http://blahblah.gif\", \"likes_count\" : 450, \"link\" : \"http://www.blahblahblah.php\", \"message\" : \"I wish I could figure this out!\", \"page_category\" : \"Computers\", \"page_id\" : \"30968999999\", \"page_name\" : \"NothingButTrouble\", \"type\" : \"photo\", \"updated_at\" : { \"$date\" : 1358210153000 } }" 
x <- paste(line.1, line.2, line.3, sep="\n")
lines <-  readLines(textConnection(x))
library(rjson)

# this is the important bit
df <- data.frame(do.call(rbind,lapply(lines,fromJSON)))
ncol(df)
# [1] 12

# finally, there's some cleaning up to do...
df$created_at
# [[1]]
# [[1]]$`$date`
# [1] 1.357942e+12
# ...
df$created_at <- as.POSIXlt(unname(unlist(df$created_at)/1000),origin="1970-01-01")
df$created_at
# [1] "2013-01-11 17:05:38 EST" "2013-01-11 17:05:38 EST" "2013-01-11 17:05:38 EST"

df$updated_at <- as.POSIXlt(unname(unlist(df$updated_at)/1000),origin="1970-01-01")

请注意,此转换假定日期存储为自纪元以来的毫秒数。

【讨论】:

  • 上述金龙的解决方案更适合读取数据。但是将“行”适应数据框的代码行非常有帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 2022-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多