Convert json column to new dataframe

I have a csv file and one of the columns is in json format .

this particular column in json format looks like this:

{"title":" ","body":" ","url":"thedailygreen print this healthy eating eat safe Dirty Dozen Foods page all"}

      

I read this file using read.csv in R. Now how do I create a new dataframe from this column that should have field names like title, body and url.

+3


source to share


1 answer


You can use RJSONIO package to parse column values ​​eg.

library(RJSONIO)

# create an example data.frame with a json column
cell1 <- '{"title":"A","body":"X","url":"http://url1.x"}'
cell2 <- '{"title":"B","body":"Y","url":"http://url2.y"}'
cell3 <- '{"title":"C","body":"Z","url":"http://url3.z"}'

df <- data.frame(jsoncol = c(cell1,cell2,cell3),stringsAsFactors=F)

# parse json and create a data.frame
res <- do.call(rbind.data.frame,
               lapply(df$jsoncol, FUN=function(x){ as.list(fromJSON(x))}))

> res
   title body           url
     A    X   http://url1.x
     B    Y   http://url2.y
     C    Z   http://url3.z

      

NB: The above code assumes that all cells contain only title, body and url. If there may be other properties in json cells, then use this code instead:

vals <- lapply(df$jsoncol,fromJSON)
res <- do.call(rbind, lapply(vals,FUN=function(v){ data.frame(title=v['title'],
                                                              body =v['body'],
                                                              url  =v['url']) }))

      

EDIT (as per comment):



I read the file using the following code:

df <- read.table(file="c:\\sample.tsv", 
                 header=T, sep="\t", colClasses="character")

      

then parsed using this code:

# define a simple function to turn NULL to NA
naIfnull <- function(x){if(!is.null(x)) x else NA}

vals <- lapply(df$boilerplate,fromJSON)
res <- do.call(rbind, 
               lapply(vals,FUN=function(v){ v <- as.list(v)
                                            data.frame(title=naIfnull(v$title),
                                                       body =naIfnull(v$body),
                                                       url  =naIfnull(v$url)) }))

      

+6


source







All Articles