0

I have a data frame in R that has some normal columns and one column that I have created using toJSON that pulled in all columns of a data frame into one json column by row using

sapply(split(json.column, seq(nrow(json.column))), toJSON, pretty=TRUE)

and then using cbind to put the columns back into my data frame by row.

Looking at this column in R looks beautiful without any encoding. When I output this file to load into a database, it adds a significant amount of seemingly arbitrary backslashes (more than just " and [ ]) and encoding, which I need removed in order to load into the database.

output I have:

columna      columnb       columnc         
abc123      12345         [\n  {\n    \"ID\":\\"GEORGE\\",\\\"SEX\\\":\\\\\\\"Male\\\\\\",\\\\\"AGE\\\\":46,\"GEO\",\"USA\"  \\n  }\\n]\"\n  }\n]"    
.....

output I want:

columna      columnb       columnc         
abc123      12345         {"ID":"GEORGE","SEX":"Male","AGE":46,"GEO","USA"}        
.....

I've tried str_remove and gsub and even cat, but the output file column (columnc in my example) doesn't seem to stay json in the column when I morph it.

I've tried other means of making my json column and can't find another way...

two days later and no luck. Any help would be greatly appreciated!

MrFlick
  • 178,638
  • 15
  • 253
  • 268
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly is in `json.column` and how are you using `cbind` and what packages are you using for the `toJSON` function? – MrFlick Mar 18 '22 at 19:52

0 Answers0