【问题标题】:multiple JSON objects into R from one txt file多个 JSON 对象从一个 txt 文件到 R
【发布时间】:2018-08-03 10:32:28
【问题描述】:

我对 Json 文件非常陌生。我抓取了一个包含数百万个 json 对象的 txt 文件,例如:

{
    "created_at":"Mon Oct 14 21:04:25 +0000 2013",
    "default_profile":true,
    "default_profile_image":true,
    "description":"...",
    "followers_count":5,
    "friends_count":560,
    "geo_enabled":true,
    "id":1961287134,
    "lang":"de",
    "name":"Peter Schmitz",
    "profile_background_color":"C0DEED",
    "profile_background_image_url":"http://abs.twimg.com/images/themes", 
    "utc_offset":-28800,
    ...
}
{
    "created_at":"Fri Oct 17 20:04:25 +0000 2015",
    ...
}

我想将列提取到 R 中的数据框中:

Variable          Value
created_at          X     
default_profile     Y     

 …

一般来说,类似于 Python 中的此处 (multiple Json objects in one file extract by python)。如果有人有想法或建议,将不胜感激!谢谢!

【问题讨论】:

标签: json r twitter


【解决方案1】:

这是一个示例,说明如何使用两个对象来处理它。我假设您能够从文件中读取 JSON,否则请参阅 here

myjson = '{"created_at": "Mon Oct 14 21:04:25 +0000 2013", "default_profile": true, 
  "default_profile_image": true, "description": "...", "followers_count": 
    5, "friends_count": 560, "geo_enabled": true, "id": 1961287134, "lang":  
    "de", "name": "Peter Schmitz", "profile_background_color": "C0DEED",  
  "profile_background_image_url": "http://abs.twimg.com/images/themes", "utc_offset": -28800}
{"created_at": "Mon Oct 15 21:04:25 +0000 2013", "default_profile": true, 
  "default_profile_image": true, "description": "...", "followers_count": 
    5, "friends_count": 560, "geo_enabled": true, "id": 1961287134, "lang":  
    "de", "name": "Peter Schmitz", "profile_background_color": "C0DEED",  
  "profile_background_image_url": "http://abs.twimg.com/images/themes", "utc_offset": -28800}
'

library("rjson")

# Split the text into a list of all JSON objects. I chose '!x!x!' pretty randomly.. There may be better ways of keeping the brackets wile splitting.
my_json_objects = head(strsplit(gsub('\\}','\\}!x!x!', myjson),'!x!x!')[[1]],-1)
# read the text as JSON objects 
json_data <- lapply(my_json_objects, function(x) {fromJSON(x)})
# Transform to dataframes
json_data <- lapply(json_data, function(x) {data.frame(val=unlist(x))}) 

输出:

[[1]]
                                                            val
created_at                       Mon Oct 14 21:04:25 +0000 2013
default_profile                                            TRUE
default_profile_image                                      TRUE
description                                                 ...
followers_count                                               5
friends_count                                               560
geo_enabled                                                TRUE
id                                                   1961287134
lang                                                         de
name                                              Peter Schmitz
profile_background_color                                 C0DEED
profile_background_image_url http://abs.twimg.com/images/themes
utc_offset                                               -28800

[[2]]
                                                            val
created_at                       Mon Oct 15 21:04:25 +0000 2013
default_profile                                            TRUE
default_profile_image                                      TRUE
description                                                 ...
followers_count                                               5
friends_count                                               560
geo_enabled                                                TRUE
id                                                   1961287134
lang                                                         de
name                                              Peter Schmitz
profile_background_color                                 C0DEED
profile_background_image_url http://abs.twimg.com/images/themes
utc_offset                                               -28800

希望这会有所帮助!

【讨论】:

  • OP 专门询问其中包含多个 JSON 对象的文件(即无效的 JSON 文件)。
  • @KonradRudolph 感谢您的反馈。我对 JSON 有点陌生,所以我不知道这是问题所在。我已经更新了我的答案,你认为这更好地回答了 OP 的问题吗?
  • 非常感谢,完美运行!只是添加可能有用的内容:如果您想将其用作完整表,请使用 json_data &lt;- lapply(json_data,t); df &lt;- plyr::ldply(json_data, rbind)
  • @TSpinde 我认为如果你用json_data &lt;- lapply(json_data, function(x) {data.frame(x)}) 替换我的代码的最后一行,你甚至可以跳过转置步骤。无论如何,很高兴我能提供帮助,并且很高兴您能够进行修改以使其适合您!
猜你喜欢
  • 2016-01-06
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多