【问题标题】:Nasty nested JSON to data frame in R讨厌的嵌套JSON到R中的数据框
【发布时间】:2017-02-28 14:02:00
【问题描述】:

我有几个 JSON 输入,如下所示。

输入1:

{"booking":{"id":"54092","createdOn":"2016-10-06T06:29:00.0000000+0000","bookingDateTime":"2016-10-08T06:45:00.0000000+0000","checkInDateTime":null,"checkOutDateTime":null,"tableNumbers":null,"pax":3,"type":"calling","status":"booked","source":"concierge","specialInstruction":"test hoppi","outlet":{"id":"46a97c2e-c921-4339-819c-50e17f25d09a","name":"Hoppipolla"},"customer":{"name":"Hoppi","email":"hoppi@xmail.com","countryCallingCode":"91","phone":"8901234567"}},"eventType":"booked","serverDateTime":"2016-10-06T06:28:25.0000558+0000"}

{"booking":{"id":"54093","createdOn":"2016-10-06T06:30:00.0000000+0000","bookingDateTime":"2016-10-08T06:46:00.0000000+0000","checkInDateTime":null,"checkOutDateTime":null,"tableNumbers":null,"pax":3,"type":"calling","status":"booked","source":"concierge","specialInstruction":"test hoppi","outlet":{"id":"46a97c2e-c922-4339-819c-50e17f25d09a","name":"Hoppipolla"},"customer":{"name":"Ravi","email":"ravi@xmail.com","countryCallingCode":"91","phone":"8901234548"}},"eventType":"booked","serverDateTime":"2016-10-06T06:28:25.0000558+0000"}

输入2:

{"feedback":{"dateTime":"2016-10-06T09:24:05.0000730+0000","customerName":"mytestdevcog","averageRating":4,"isNegativeByComments":false,"outlet":{"id":"f9e9c21f-2e52-462e-a1f2-012dab216d27","name":"cholan 's Darbar"},"customer":{"name":"mytestdevcog","email":"mytestdevcog123@gmail.com","countryCallingCode":"91","phone":"7895478745"},"responses":[{"question":"Was your waiting time too long?","response":"No","questionType":"yes_no"},{"question":"Service","response":"3","questionType":"rating"},{"question":"desserts","response":"4","questionType":"rating"},{"question":"Comments","response":"good","questionType":"short_answer"},{"question":"How likely is it that you would recommend us to a friend or colleague?","response":"5","questionType":"nps"},{"question":"Portion Size","response":"5","questionType":"rating"},{"question":"Indian Food","response":"4","questionType":"rating"},{"question":"Desert spread","response":"4","questionType":"rating"}]},"serverDateTime":"2016-10-06T09:24:27.0000644+0000"}

{"feedback":{"dateTime":"2016-10-06T09:24:05.0000730+0000","customerName":"mytestdevcog","averageRating":4,"isNegativeByComments":false,"outlet":{"id":"f9e9c21f-2e52-462e-a1f2-012dab216d27","name":"cholan 's Darbar"},"customer":{"name":"mytestdevcog","email":"mytestdevcog123@gmail.com","countryCallingCode":"91","phone":"7895478745"},"responses":[{"question":"Was your waiting time too long?","response":"No","questionType":"yes_no"},{"question":"Service","response":"3","questionType":"rating"},{"question":"desserts","response":"4","questionType":"rating"},{"question":"Comments","response":"good","questionType":"short_answer"},{"question":"How likely is it that you would recommend us to a friend or colleague?","response":"5","questionType":"nps"},{"question":"Portion Size","response":"5","questionType":"rating"},{"question":"Indian Food","response":"4","questionType":"rating"},{"question":"Desert spread","response":"4","questionType":"rating"}]},"serverDateTime":"2016-10-06T09:24:27.0000644+0000"}

两个输入需要两个单独的数据帧。这如何在 R 中实现?

【问题讨论】:

  • 请先编辑您的 json 输入数据。看起来很奇怪。
  • jsonlite::fromJSON(input1)。但是,您需要使输入有效 JSON。而且你最好直接从源代码读入 R
  • 您的 JSON 具有未屏蔽的 PII。

标签: json r dataframe


【解决方案1】:

正如 cmets 中正确指出的那样,您的 json 包含错误。第三和第四个字符串实际上包含“cholan's darbar”,必须先对其进行转义,然后才能将其解析为json。

为了您的问题(关于获取两个数据集),我直接更正了字符串。这是如何完成的:

library(jsonlite)

s1 <- '{"booking":{"id":"54092","createdOn":"2016-10-06T06:29:00.0000000+0000","bookingDateTime":"2016-10-08T06:45:00.0000000+0000","checkInDateTime":null,"checkOutDateTime":null,"tableNumbers":null,"pax":3,"type":"calling","status":"booked","source":"concierge","specialInstruction":"test hoppi","outlet":{"id":"46a97c2e-c921-4339-819c-50e17f25d09a","name":"Hoppipolla"},"customer":{"name":"Hoppi","email":"hoppi@xmail.com","countryCallingCode":"91","phone":"8901234567"}},"eventType":"booked","serverDateTime":"2016-10-06T06:28:25.0000558+0000"}'

s2 <- '{"booking":{"id":"54093","createdOn":"2016-10-06T06:30:00.0000000+0000","bookingDateTime":"2016-10-08T06:46:00.0000000+0000","checkInDateTime":null,"checkOutDateTime":null,"tableNumbers":null,"pax":3,"type":"calling","status":"booked","source":"concierge","specialInstruction":"test hoppi","outlet":{"id":"46a97c2e-c922-4339-819c-50e17f25d09a","name":"Hoppipolla"},"customer":{"name":"Ravi","email":"ravi@xmail.com","countryCallingCode":"91","phone":"8901234548"}},"eventType":"booked","serverDateTime":"2016-10-06T06:28:25.0000558+0000"}'

在以下两个字符串中,我手动将“cholan's Darbar”更正为“cholan \'s Darbar”

s3 <- '{"feedback":{"dateTime":"2016-10-06T09:24:05.0000730+0000","customerName":"mytestdevcog","averageRating":4,"isNegativeByComments":false,"outlet":{"id":"f9e9c21f-2e52-462e-a1f2-012dab216d27","name":"cholan \'s Darbar"},"customer":{"name":"mytestdevcog","email":"mytestdevcog123@gmail.com","countryCallingCode":"91","phone":"7895478745"},"responses":[{"question":"Was your waiting time too long?","response":"No","questionType":"yes_no"},{"question":"Service","response":"3","questionType":"rating"},{"question":"desserts","response":"4","questionType":"rating"},{"question":"Comments","response":"good","questionType":"short_answer"},{"question":"How likely is it that you would recommend us to a friend or colleague?","response":"5","questionType":"nps"},{"question":"Portion Size","response":"5","questionType":"rating"},{"question":"Indian Food","response":"4","questionType":"rating"},{"question":"Desert spread","response":"4","questionType":"rating"}]},"serverDateTime":"2016-10-06T09:24:27.0000644+0000"}'

s4 <- '{"feedback":{"dateTime":"2016-10-06T09:24:05.0000730+0000","customerName":"mytestdevcog","averageRating":4,"isNegativeByComments":false,"outlet":{"id":"f9e9c21f-2e52-462e-a1f2-012dab216d27","name":"cholan \'s Darbar"},"customer":{"name":"mytestdevcog","email":"mytestdevcog123@gmail.com","countryCallingCode":"91","phone":"7895478745"},"responses":[{"question":"Was your waiting time too long?","response":"No","questionType":"yes_no"},{"question":"Service","response":"3","questionType":"rating"},{"question":"desserts","response":"4","questionType":"rating"},{"question":"Comments","response":"good","questionType":"short_answer"},{"question":"How likely is it that you would recommend us to a friend or colleague?","response":"5","questionType":"nps"},{"question":"Portion Size","response":"5","questionType":"rating"},{"question":"Indian Food","response":"4","questionType":"rating"},{"question":"Desert spread","response":"4","questionType":"rating"}]},"serverDateTime":"2016-10-06T09:24:27.0000644+0000"}'


j1 <- fromJSON(s1)
j2 <- fromJSON(s2)
j3 <- fromJSON(s3)
j4 <- fromJSON(s4)

rbind(j1, j2)

rbind(j3, j4)

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 2013-10-23
    • 1970-01-01
    • 2011-03-05
    • 2023-03-20
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多