How to convert column of a data frame in json format in R -


     email    foo    bar   1  a@g.com    23     34   2  b@g.com    43     34   3  c@g.com    35     32 

now want create json docs each email of following form:
doc 1: { "email" : "a@g.com"}
doc 2: { "email" : "b@g.com"}
doc 3: { "email" : "c@g.com"}

my final goal insert these docs in mongodb using dbinsertdocument() rmongo.
have been toying tojson() couldn't figure out way. how convert final$email in json docs described above?

i'm not sure if need: final$email has not column name not included in output of tojson(final$email)

library(jsonlite)  txt <- "email foo bar 1 a@g.com 23 34 2 b@g.com 43 34 3 c@g.com 35 32"  final <- read.table(text=txt, stringsasfactors = false) tojson(final$email) # [1] "[\"a@g.com\",\"b@g.com\",\"c@g.com\"]"      # final$email has converted data.frame dt <- data.frame(final$email) colnames(dt) <- "email" tojson(dt) # [{"email":"a@g.com"},{"email":"b@g.com"},{"email":"c@g.com"}] 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -