2016-10-19 73 views
-3

我有幾個JSON輸入如下。令人討厭的嵌套JSON到R中的數據幀

輸入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":"[email protected]","countryCallingCode":"91","phone":"89"}},"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[email protected]","countryCallingCode":"91","phone":"89"}},"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":"[email protected]","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":"[email protected]","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"} 

需要兩個輸入兩個單獨dataframes。這在R中如何實現?

+0

請先編輯您的JSON的輸入數據。它看起來很奇怪。 –

+0

'jsonlite :: fromJSON(input1)'。但是,您需要使您的輸入有效JSON。你可能會更好地直接從源讀取到R – SymbolixAU

+0

你的JSON已揭露PII。 – tchakravarty

回答

0

正如在評論中指出的那樣,你的json包含一個錯誤。第三和第四個字符串實際上包含「cholan的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":"[email protected]","countryCallingCode":"91","phone":"89"}},"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":"[email protected]","countryCallingCode":"91","phone":"89"}},"eventType":"booked","serverDateTime":"2016-10-06T06:28:25.0000558+0000"}' 
在以下兩個字符串

我手動校正「膽的Darbar」到「膽\的 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":"[email protected]","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":"[email protected]","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)