将嵌套的json转换为R中的flat数据帧

时间:2016-10-03 13:07:21

标签: json r dataframe

我的JSON结构看起来像这样:

http://www.jsoneditoronline.org/?id=5e56a90d32df774fa2beaf69bdb9f0af

我希望它转换为完全平坦的dataframe

res <- jsonlite::fromJSON(data, simplifyDataFrame=TRUE) 

  res <- jsonlite::fromJSON(data, flatten=TRUE)

给出奇怪的结果。

我听说过ndjson但它在ubuntu 14.04中抛出了不兼容的gcc错误。

有一种强有力的方法吗?我也试过jsonlite的stream_in,但它也给出了奇怪的结果。

1 个答案:

答案 0 :(得分:0)

在github页面上有ndjson的构建说明,如果你查看INSTALL文件。但是,您没有ndjson,您有一个JSON数组的复杂JSON对象,可以由jsonlite轻松处理:

数据:

txt <- '[
  {
    "agent": {
      "active_since": null,
      "available": false,
      "created_at": "2016-09-29T14:47:00+05:30",
      "id": 1000124609,
      "last_active_at": null,
      "occasional": false,
      "points": 100,
      "scoreboard_level_id": 1000316008,
      "signature": null,
      "signature_html": "<div dir=\\"ltr\\">\\n<p><br></p>\\r\\n</div>",
      "ticket_permission": 1,
      "updated_at": "2016-09-30T17:57:59+05:30",
      "user_id": 1013512877,
      "user": {
        "active": false,
        "address": null,
        "created_at": "2015-06-29T11:51:04+05:30",
        "deleted": false,
        "description": null,
        "email": "aakash.r@stalkbuylove.com",
        "external_id": null,
        "fb_profile_id": null,
        "helpdesk_agent": true,
        "id": 1013512877,
        "job_title": "",
        "language": "en",
        "mobile": null,
        "name": "Aakash Rathore",
        "phone": null,
        "time_zone": "New Delhi",
        "twitter_id": null,
        "updated_at": "2016-09-29T14:47:23+05:30"
      }
    }
  },
  {
    "agent": {
      "active_since": null,
      "available": false,
      "created_at": "2016-09-29T14:48:34+05:30",
      "id": 1000124610,
      "last_active_at": null,
      "occasional": false,
      "points": 100,
      "scoreboard_level_id": 1000316008,
      "signature": null,
      "signature_html": "<div dir=\\"ltr\\">\\n<p><br></p>\\r\\n</div>",
      "ticket_permission": 1,
      "updated_at": "2016-09-30T18:02:30+05:30",
      "user_id": 1021869364,
      "user": {
        "active": false,
        "address": null,
        "created_at": "2016-08-09T15:18:06+05:30",
        "deleted": false,
        "description": null,
        "email": "abhinav.a@stalkbuylove.com",
        "external_id": null,
        "fb_profile_id": null,
        "helpdesk_agent": true,
        "id": 1021869364,
        "job_title": "",
        "language": "en",
        "mobile": null,
        "name": "Abhinav Anand",
        "phone": null,
        "time_zone": "New Delhi",
        "twitter_id": null,
        "updated_at": "2016-09-29T14:48:52+05:30"
      }
    }
  }
]'

代码:

library(jsonlite)

dplyr::glimpse(fromJSON(txt, flatten=TRUE))
##     Observations: 2
## Variables: 31
## $ agent.active_since        <lgl> NA, NA
## $ agent.available           <lgl> FALSE, FALSE
## $ agent.created_at          <chr> "2016-09-29T14:47:00+05:30", "2016-09-29T14:48:34+05:30"
## $ agent.id                  <int> 1000124609, 1000124610
## $ agent.last_active_at      <lgl> NA, NA
## $ agent.occasional          <lgl> FALSE, FALSE
## $ agent.points              <int> 100, 100
## $ agent.scoreboard_level_id <int> 1000316008, 1000316008
## $ agent.signature           <lgl> NA, NA
## $ agent.signature_html      <chr> "<div dir=\"ltr\">\n<p><br></p>\r\n</div>", "<div dir=\"ltr\...
## $ agent.ticket_permission   <int> 1, 1
## $ agent.updated_at          <chr> "2016-09-30T17:57:59+05:30", "2016-09-30T18:02:30+05:30"
## $ agent.user_id             <int> 1013512877, 1021869364
## $ agent.user.active         <lgl> FALSE, FALSE
## $ agent.user.address        <lgl> NA, NA
## $ agent.user.created_at     <chr> "2015-06-29T11:51:04+05:30", "2016-08-09T15:18:06+05:30"
## $ agent.user.deleted        <lgl> FALSE, FALSE
## $ agent.user.description    <lgl> NA, NA
## $ agent.user.email          <chr> "aakash.r@stalkbuylove.com", "abhinav.a@stalkbuylove.com"
## $ agent.user.external_id    <lgl> NA, NA
## $ agent.user.fb_profile_id  <lgl> NA, NA
## $ agent.user.helpdesk_agent <lgl> TRUE, TRUE
## $ agent.user.id             <int> 1013512877, 1021869364
## $ agent.user.job_title      <chr> "", ""
## $ agent.user.language       <chr> "en", "en"
## $ agent.user.mobile         <lgl> NA, NA
## $ agent.user.name           <chr> "Aakash Rathore", "Abhinav Anand"
## $ agent.user.phone          <lgl> NA, NA
## $ agent.user.time_zone      <chr> "New Delhi", "New Delhi"
## $ agent.user.twitter_id     <lgl> NA, NA
## $ agent.user.updated_at     <chr> "2016-09-29T14:47:23+05:30", "2016-09-29T14:48:52+05:30"