Error while trying to parse json into R

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:31:49

问题


I have recently started using R and have a task regarding parsing json in R to get a non-json format. For this, i am using the "fromJSON()" function. I have tried to parse json as a text file. It runs successfully when i do it with just a single row entry. But when I try it with multiple row entries, i get the following error:

 fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
  lexical error: invalid char in json text.
                                     [{'CategoryType':'dining','City':
                     (right here) ------^

> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
  parse error: trailing garbage
          "mumbai","Location":"all"}]  [{"JourneyType":"Return","Origi
                     (right here) ------^

> fromJSON("D:/Eclairs/Printing/test3.txt")
Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
  parse error: after array element, I expect ',' or ']'
          :"mumbai","Location":"all"}  {"JourneyType":"Return","Origin
                     (right here) ------^

The above errors are due to three different formats in which i tried to parse the json text, but the result was the same, only the location suggested by changed. Please help me to identify the cause of this error or if there is a more efficient way o performing the task.

The original file that i have is an excel sheet with multiple columns and one of those columns consists of json text. The way i tried right now is by extracting just the json column and converting it to a tab separated text and then parsing it as:

fromJSON("D:/Eclairs/Printing/test3.txt")

Please also suggest if this can be done more efficiently. I need to map all the columns in the excel to the non-json text as well.

Example: 
[{"CategoryType":"dining","City":"mumbai","Location":"all"}]
[{"CategoryType":"reserve-a-table","City":"pune","Location":"Kothrud,West Pune"}]
[{"Destination":"Mumbai","CheckInDate":"14-Oct-2016","CheckOutDate":"15-Oct-2016","Rooms":"1","NoOfPax":"3","NoOfAdult":"3","NoOfChildren":"0"}]

回答1:


Consider reading in the text line by line with readLines(), iteratively saving the JSON dataframes to a growing list:

library(jsonlite)

con <- file("C:/Path/To/Jsons.txt", open="r")

jsonlist <- list()
while (length(line <- readLines(con, n=1, warn = FALSE)) > 0) {
  jsonlist <- append(jsonlist, list(fromJSON(line)))
}
close(con)

jsonlist    
# [[1]]
#   CategoryType   City Location
# 1       dining mumbai      all

# [[2]]
#      CategoryType City          Location
# 1 reserve-a-table pune Kothrud,West Pune

# [[3]]
#   Destination CheckInDate CheckOutDate Rooms NoOfPax NoOfAdult NoOfChildren
# 1      Mumbai 14-Oct-2016  15-Oct-2016     1       3         3            0


来源:https://stackoverflow.com/questions/40448368/error-while-trying-to-parse-json-into-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!