json - 在 R 中从 JSON 转换为数据帧

标签 json r

我在将 JSON 转换为数据帧时遇到了问题。我尝试使用库:jsonlite , RJSONIO , rjson .

我不断收到“字符串中的无效字符”或未关闭的字符串。

我从标准 API 获取此数据,因此应该能够解析此 json。此外,JSON 编辑器可以很好地解析这些数据。

我的问题是:

是否有一种标准方法可以确保我的数据帧被创建并忽略上述错误?

我最好的猜测是使用 toJSON 将此数据转换为 JSON 格式。来自任何一个库的函数,但如果我使用

   newdata <-  fromJSON(toJSON(data))

它以某种方式永远不会转换为数据帧。这是为什么?

如果我改为使用
  newdata <- fromJSON(data)

我得到了一个有效的数据帧,但有时由于上述错误,它不起作用,这正是我想知道的。你如何处理这个问题?

我也试过用这个 freshDeskTicketsToDF <-
jsonlite::fromJSON(paste(readLines(textConnection(freshDeskTickets)), collapse=""))
  • 它似乎解决了问题,但在某处我用这种方法得到了未闭合的字符串,否则我没有。

  • 在 R 中有没有更好的方法来处理这个问题?
  • 另外,为什么在传递给 fromJSON 的数据上使用 toJSON 永远不会转换为数据帧?
  • 如果我决定从分配给 JSON 数据中的键的值中去除 html 标签。这是如何运作的?我可以这样做吗?

  • 编辑:当我有 <html tags> 时,我似乎收到此错误在我的“字符串数据”中,但我在我的 JSON 数据中都有它们,而且我不是每次都得到它。

    遇到这样的问题怎么办?

    注意:此问题并非特定于我拥有的数据。我正在寻找的是处理此类问题的方法,而不是针对单个问题的特定解决方案。
  • 我刚刚意识到 toJSON 将 R 对象转换为 JSON 而不是 JSON 转换为有效的 JSON。有没有办法做到这一点?

  • 样本数据:
    [
      {
        "cc_emails": [
    
        ],
        "fwd_emails": [
    
        ],
        "reply_cc_emails": [
    
        ],
        "fr_escalated": false,
        "spam": false,
        "email_config_id": 1000062780,
        "group_id": 1000179078,
        "priority": 1,
        "requester_id": 1022205968,
        "responder_id": 1018353725,
        "source": 1,
        "company_id": null,
        "status": 5,
        "subject": "Order number-100403891",
        "to_emails": [
          "contact@stalkbuylove.com"
        ],
        "product_id": null,
        "id": 174093,
        "type": "Order Status query",
        "due_by": "2016-09-02T08:57:30Z",
        "fr_due_by": "2016-09-02T02:57:30Z",
        "is_escalated": true,
        "description": "<div dir=\"ltr\">Hi Team,<div><br></div>\n<div>I have ordered an item from your website, order number-100403891. I had called on August 30 2016 to postpone the delivery date. The guy i spoke from your end had confirmed that he will hold and push the delivery date to September 5 or 6 or 7 2016. And he confirmed the same.</div>\n<div>However, the guy I spoke to<b> did not do it</b>. </div>\n<div>I got to know it from ABHINAV from your customer care team who I spoke to on August 1st at 13:10. Hence I have put a request again and he said he will talk to some guys and give me the desired dates for delivery which is 5,6,7 of September 2016. </div>\n<div>Please let me know the concern on this and hope for a quick turn around.</div>\n<div><br></div>\n<div>Thank you,</div>\n<div>Hari,</div>\n<div>+91-9538199699.</div>\n</div>\n",
        "description_text": "Hi Team,\r\n\r\nI have ordered an item from your website, order number-100403891. I had\r\ncalled on August 30 2016 to postpone the delivery date. The guy i spoke\r\nfrom your end had confirmed that he will hold and push the delivery date to\r\nSeptember 5 or 6 or 7 2016. And he confirmed the same.\r\nHowever, the guy I spoke to* did not do it*.\r\nI got to know it from ABHINAV from your customer care team who I spoke to\r\non August 1st at 13:10. Hence I have put a request again and he said he\r\nwill talk to some guys and give me the desired dates for delivery which is\r\n5,6,7 of September 2016.\r\nPlease let me know the concern on this and hope for a quick turn around.\r\n\r\nThank you,\r\nHari,\r\n+91-9538199699.\n",
        "custom_fields": {
    
        },
        "created_at": "2016-09-01T07:51:18Z",
        "updated_at": "2016-09-11T11:00:33Z"
      },
      {
        "cc_emails": [
    
        ],
        "fwd_emails": [
    
        ],
        "reply_cc_emails": [
    
        ],
        "fr_escalated": false,
        "spam": false,
        "email_config_id": 1000062780,
        "group_id": 1000179078,
        "priority": 1,
        "requester_id": 1022148025,
        "responder_id": 1021145209,
        "source": 1,
        "company_id": null,
        "status": 5,
        "subject": "Defect in d piece",
        "to_emails": [
          "contact@stalkbuylove.com"
        ],
        "product_id": null,
        "id": 174092,
        "type": "Return",
        "due_by": "2016-09-01T15:51:00Z",
        "fr_due_by": "2016-09-01T09:51:00Z",
        "is_escalated": false,
        "description": "<div><br></div>\n<div><br></div>\n<div><br></div>\n<div><div style=\"font-size:75%;color:#575757\">Sent from Samsung Mobile</div></div>",
        "description_text": "\n\n\nSent from Samsung Mobile",
        "custom_fields": {
    
        },
        "created_at": "2016-09-01T07:51:00Z",
        "updated_at": "2016-09-06T09:00:14Z"
      },
      {
        "cc_emails": [
    
        ],
        "fwd_emails": [
    
        ],
        "reply_cc_emails": [
    
        ],
        "fr_escalated": false,
        "spam": false,
        "email_config_id": 1000062780,
        "group_id": 1000179078,
        "priority": 1,
        "requester_id": 1022205895,
        "responder_id": 1018353725,
        "source": 1,
        "company_id": null,
        "status": 5,
        "subject": "Re: StalkBuyLove Return Request for order: 100404435",
        "to_emails": [
          "StalkBuyLove <contact@stalkbuylove.com>"
        ],
        "product_id": null,
        "id": 174088,
        "type": "Refund query",
        "due_by": "2016-09-01T15:43:56Z",
        "fr_due_by": "2016-09-01T09:43:56Z",
        "is_escalated": true,
        "description": "<div>Hi. Can u deposit the amount if i giv u my account number. Right away i cant choose any other product frim ur site. <br><br>Sent from my iPhone</div>\n<div>\n<br>On Sep 1, 2016, at 12:38 PM, StalkBuyLove &lt;<a href=\"mailto:contact@stalkbuylove.com\" rel=\"noreferrer\">contact@stalkbuylove.com</a>&gt; wrote:<br><br>\n</div>\n<blockquote><div>\n<div><img title=\"StalkBuyLove\" alt=\"Stalkbuylove\" src=\"http://www.stalkbuylove.com/launcher_icons/Newlogo_Stalkbuylove_240x50.png\"></div>\n<div>Hello <b>Anamica Aggarwal</b>,</div>\n<div>We have initiated a return request for order: <b>100404435</b> with the following products:</div>\n<table style=\"width:80%\">\r\n                                <tbody>\n<tr style=\"background-color:#B0C4DE\">\r\n                                   <th>Item Name</th>\r\n                                   <th>Sku</th>\r\n                                </tr>\n<tr>\r\n                                        <td style=\"text-align:center\">Articuno Top</td>\r\n                                        <td style=\"text-align:center\">IN1627MTOTOPPCH-198-18</td>\r\n                                   </tr>\n</tbody>\n</table>\n<div>Lots of love,</div>\n<div>Team SBL</div>\n<img src=\"http://mandrillapp.com/track/open.php?u=30069003&amp;id=bff0a5daee4a47fe9c6b04d2680c3c39\" height=\"1\" width=\"1\">\r\n</div></blockquote>",
        "description_text": "Hi. Can u deposit the amount if i giv u my account number. Right away i cant choose any other product frim ur site. \n\nSent from my iPhone\n\n> On Sep 1, 2016, at 12:38 PM, StalkBuyLove <contact@stalkbuylove.com> wrote:\n> \n> \n> Hello Anamica Aggarwal,\n> \n> We have initiated a return request for order: 100404435 with the following products:\n> \n> Item Name\tSku\n> Articuno Top\tIN1627MTOTOPPCH-198-18\n> Lots of love,\n> \n> Team SBL\n> \n",
        "custom_fields": {
    
        },
        "created_at": "2016-09-01T07:43:56Z",
        "updated_at": "2016-09-11T11:00:32Z"
      },
      {
        "cc_emails": [
    
        ],
        "fwd_emails": [
    
        ],
        "reply_cc_emails": [
    
        ],
        "fr_escalated": false,
        "spam": false,
        "email_config_id": 1000062780,
        "group_id": 1000179078,
        "priority": 1,
        "requester_id": 1022205881,
        "responder_id": 1021145209,
        "source": 1,
        "company_id": null,
        "status": 5,
        "subject": "Details for order",
        "to_emails": [
          "contact@stalkbuylove.com"
        ],
        "product_id": null,
        "id": 174086,
        "type": "Order Status query",
        "due_by": "2016-09-01T15:42:50Z",
        "fr_due_by": "2016-09-01T09:42:50Z",
        "is_escalated": false,
        "description": "<div><span></span></div>\n<div>\n<span>Hey can i get details of my order </span><br><span>How much more time will it take to get delivered? </span><br><span>Order no-</span><h2 style=\"font-weight: normal; margin: 0px;\"><font><span style=\"background-color: rgba(255, 255, 255, 0);\">100403837</span></font></h2>\n<span></span><br><span>Sent from my iPhone</span><br>\n</div>",
        "description_text": "Hey can i get details of my order \r\nHow much more time will it take to get delivered? \r\nOrder no-\r\n100403837\r\n\r\nSent from my iPhone\n",
        "custom_fields": {
    
        },
        "created_at": "2016-09-01T07:42:50Z",
        "updated_at": "2016-09-06T09:00:13Z"
      },
      {
        "cc_emails": [
    
        ],
        "fwd_emails": [
    
        ],
        "reply_cc_emails": [
    
        ],
        "fr_escalated": true,
        "spam": false,
        "email_config_id": 1000062780,
        "group_id": 1000179078,
        "priority": 1,
        "requester_id": 1022204690,
        "responder_id": 1021145209,
        "source": 1,
        "company_id": null,
        "status": 5,
        "subject": "Refund",
        "to_emails": [
          "contact@stalkbuylove.com"
        ],
        "product_id": null,
        "id": 174080,
        "type": "Refund query",
        "due_by": "2016-09-01T15:36:26Z",
        "fr_due_by": "2016-09-01T09:36:26Z",
        "is_escalated": true,
        "description": "<div>\r<br>Bank statement as asked for refund! Please intiate the proccedings asap!<br>\n</div>",
        "description_text": "\r\nBank statement as asked for refund! Please intiate the proccedings asap!\n",
        "custom_fields": {
    
        },
        "created_at": "2016-09-01T07:36:26Z",
        "updated_at": "2016-09-07T08:00:19Z"
      }
    ]
    

    最佳答案

    library(jsonlite)
    
    df <- stream_in(file("~/data/sample.json"))
    
    stream_in函数直接转换为dataframe

    关于json - 在 R 中从 JSON 转换为数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737856/

    相关文章:

    json - 从数据库存储和查询 JSON

    php - 使用json更新mysql数据库空白字段

    json - 从包含映射(键值对)的复杂 JSON 创建 Avro 模式

    r - 如何计算R中的加权和?

    r - 如何在包中使用 rlang 运算符?

    java - 从 Rest-Webservice 发送 Json [Gson 实现]

    javascript - 在 JSON 数组中查找项目并将其添加到另一个数组中。 JavaScript

    R:二进制矩阵的索引列表

    r - 如何在每个条形顶部显示百分比标签

    r - Plotly 不显示线条