使用R,JSONLITE和DPLYR通过适当的转换在数据框中转换JSON

时间:2019-01-30 15:07:04

标签: r dataframe dplyr jsonlite

在R中,我有一个想要转换为数据帧的JSON响应:

{
storeCode:1,
language:"en",
languageCode:"9",
market:"gb",
  offices: [
    {
    officeId:1,
    siteId:1,
    streetName: "xx xxxxxxx xx",
    postalCode: "001234",
    town:"NY",
    region:"APAC",
    customer: [
      {
      id: 1232,
      name: "John Doe",
      note: "The good",
      value: 1
      },
      {
      id: 1233,
      name: "Tim Doe",
      note: "The Bad",
      value: 1
      },
      {
      id: 1234,
      name: "Smith Doe",
      note: "The Ugly",
      value: 1
      }
    ]
    },
    {
    officeId:1,
    siteId: 2,
    streetName: "xx xxxxxxx xx",
    postalCode: "001234",
    town:"NY",
    region:"APAC",
    customer: [
      {
      id: 1235,
      name: "Tim Doe",
      note: "Ocus",
      value: 1
      },
      {
      id: 1236,
      name: "Gin Doe",
      note: "Pocus",
      value: 1
      }
    ]
    }
  ]
}

此刻,我使用JSONLITE库转换为列表对象。 现在,我想将列表转换为数据框,其中JSON元素中的所有数组将按以下方式按列转换:

|storeCode | language | languageCode | market | offices.officeId | offices.siteId | offices.streetName | offices.postalCode | offices.town | offices.region | customer.id | customer.name | customer.note | customer.value |
|    1     |    en    |       9      |   gb   |         1        |        1       | xx xxxxxxx xx      |      001234        |       NY     |      APAC      |    1232     |    John Doe   |    The good   |        1       |
|    1     |    en    |       9      |   gb   |         1        |        1       | xx xxxxxxx xx      |      001234        |       NY     |      APAC      |    1233     |    Tim Doe    |    The bad    |        2       |
|    1     |    en    |       9      |   gb   |         1        |        1       | xx xxxxxxx xx      |      001234        |       NY     |      APAC      |    1234     |    Smith Doe  |    The ugly   |        4       |
|    1     |    en    |       9      |   gb   |         1        |        2       | xx xxxxxxx xx      |      001235        |       NY     |      APAC      |    1235     |    Tim Doe    |    Ocus       |        1       |
|    1     |    en    |       9      |   gb   |         1        |        2       | xx xxxxxxx xx      |      001236        |       NY     |      APAC      |    1236     |    Gin Doe    |    Pocus      |        1       |

我尝试使用unestest功能没有任何运气。我对R相当陌生,所以我真的不知道从哪里开始。

任何帮助将不胜感激。 谢谢 。:M :.

0 个答案:

没有答案