0

I have my Rstudio connected to a MySQL database. The table I'm importing has a MySQL JSON column type: https://dev.mysql.com/doc/refman/5.7/en/json.html

When I import it into R, it becomes a BLOb. You can see the table, as its imported, here:

'data.frame':   15 obs. of  5 variables:  
$ id :integer64 1 2 3 4 5 6 7 8 ...   
$ user_id : chr  
$ survey_id:integer64 3 10 10 10 10 3 10 10 ...   
$ p_id : chr  "22zdae" "0" "0" "0" ... 
$ data : blob [1:15]    ..$ : raw  7b 22 45 78 ...

When I go to extract information from the blob I use the following code:

     for(row in 1:NROW(data)){
   print(row)
   tryCatch({
     if(is_empty(data$data[[row]])==TRUE){
       x<-NA
     } else {
       x <- rawToChar(data$data[[row]])
     }
     survey_data <- rbind(survey_data,x)
   }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")}
   )}

Every row is transformed into only partially what in the database. For example:

Status": "Never married", "Liberal_Conserv": "Very Liberal", "Political_Party": "Republican", "Kids_18yo_Number": ""}

This row has 251 variables in the database, not 4.

How can I accurately transform a blob into workable data?

IRTFM
  • 251,731
  • 20
  • 347
  • 472

0 Answers0