2016-10-03 67 views
0

JSON struture看起來是這樣的:轉換嵌套的JSON平數據幀中的R

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,但它也給出了奇怪的結果。

回答

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": "[email protected]", 
     "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": "[email protected]", 
     "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> "[email protected]", "[email protected]" 
## $ 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" 
+0

是的,那是什麼它看起來像但這不是我所需要的。我一直在寫數據框爲CSV。我該怎麼做這個? – gags

+0

我的意思是這個JSON對象數組中的每個對象都必須是CSV文件中的一行 – gags