Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a column in json format into a new data frame

Tags:

json

r

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

that particular column in json format looks like this:

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

I have read this file using read.csv in R. Now, how to I create a new data frame from this column which should have field names as title, body and url.

like image 671
user3928988 Avatar asked Dec 30 '25 18:12

user3928988


1 Answers

You can use package RJSONIO to parse the column values, e.g. :

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

N.B. : the code above assumes all the cells contains title, body and url only. If there can be other properties in the json cells, 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've 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)) }))
like image 53
digEmAll Avatar answered Jan 01 '26 06:01

digEmAll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!