R convert dataframe to JSON
Asked Answered
W

5

49

I have a dataframe that I'd like to convert to json format:

my data frame called res1:

library(rjson)

structure(list(id = c(1, 2, 3, 4, 5), value = structure(1:5, .Label = c("server1", 
"server2", "server3", "server4", "server5"), class = "factor")), .Names = c("id", 
"value"), row.names = c(NA, -5L), class = "data.frame")

when I do:

toJSON(res1)

I get this:

{"id":[1,2,3,4,5],"value":["server1","server2","server3","server4","server5"]}

I need this json output to be like this, any ideas?

[{"id":1,"value":"server1"},{"id":2,"value":"server2"},{"id":3,"value":"server3"},{"id":4,"value":"server4"},{"id":5,"value":"server5"}]
Whitener answered 28/8, 2014 at 13:59 Comment(5)
Why do you think you want it in that format? jsonlint.com says it's not valid.Blunderbuss
@doctorlove, I've update the json format I need to see. Thank you.Whitener
Still not valid ;-) Parse error on line 20: ... 5, "value:"server5"}] ----------------------^ Expecting '}', ':', ',', ']'Blunderbuss
@doctorlove, I missed ", updated.Whitener
Better :-) Do consider using jsonlint.com just to check in future.Blunderbuss
E
35

How about

library(rjson)
x <- toJSON(unname(split(res1, 1:nrow(res1))))
cat(x)
# [{"id":1,"value":"server1"},{"id":2,"value":"server2"},
# {"id":3,"value":"server3"},{"id":4,"value":"server4"},
# {"id":5,"value":"server5"}]

By using split() we are essentially breaking up the large data.frame into a separate data.frame for each row. And by removing the names from the resulting list, the toJSON function wraps the results in an array rather than a named object.

Edmead answered 28/8, 2014 at 14:56 Comment(1)
Thanks. And if we want split by row. library(rjson);library(dplyr);res1<-data.frame(id= c(1, 2, 3, 4, 5),server=c("server1", "server2", "server3", "server4", "server5"),y1=letters[1:5],y2=LETTERS[1:5]) with res1$JsonList<-lapply ( (unname( split( (res1 %>% select_at (vars(starts_with("y")))), 1:nrow(res1)) ) ), function(x) rjson::toJSON(x) ) and with res1 %>% select_at (vars(-starts_with("y")))Fouquiertinville
H
66

The jsonlite package exists to address exactly this problem: "A practical and consistent mapping between JSON data and R objects."

Its toJSON function provides this desired result with the default options:

library(jsonlite)
x <- toJSON(res1)
cat(x)

## [{"id":1,"value":"server1"},{"id":2,"value":"server2"},
## {"id":3,"value":"server3"},{"id":4,"value":"server4"},
## {"id":5,"value":"server5"}]
Hanna answered 2/7, 2015 at 20:35 Comment(1)
if you have the issue, that you have your values nested within an array, instead of a single value: use the option auto_unbox=TRUE in the toJSON function to unbox "value":["server1"] to "value": "server1". example command: toJSON(res1, pretty=TRUE, flatten=TRUE, auto_unbox=TRUE)Disturbing
E
35

How about

library(rjson)
x <- toJSON(unname(split(res1, 1:nrow(res1))))
cat(x)
# [{"id":1,"value":"server1"},{"id":2,"value":"server2"},
# {"id":3,"value":"server3"},{"id":4,"value":"server4"},
# {"id":5,"value":"server5"}]

By using split() we are essentially breaking up the large data.frame into a separate data.frame for each row. And by removing the names from the resulting list, the toJSON function wraps the results in an array rather than a named object.

Edmead answered 28/8, 2014 at 14:56 Comment(1)
Thanks. And if we want split by row. library(rjson);library(dplyr);res1<-data.frame(id= c(1, 2, 3, 4, 5),server=c("server1", "server2", "server3", "server4", "server5"),y1=letters[1:5],y2=LETTERS[1:5]) with res1$JsonList<-lapply ( (unname( split( (res1 %>% select_at (vars(starts_with("y")))), 1:nrow(res1)) ) ), function(x) rjson::toJSON(x) ) and with res1 %>% select_at (vars(-starts_with("y")))Fouquiertinville
S
7

Now you can easily just call jsonlite::write_json() directly on the dataframe.

Secretion answered 30/12, 2017 at 7:36 Comment(1)
read_json and write_json are designed to read/write from disk. jsonlite::toJSON(res1) is still the correct answer for this question.Carcanet
C
1

You can also use library(jsonify)

jsonify::to_json( res1 )
# [{"id":1.0,"value":"server1"},{"id":2.0,"value":"server2"},{"id":3.0,"value":"server3"},{"id":4.0,"value":"server4"},{"id":5.0,"value":"server5"}]
Carcanet answered 12/7, 2019 at 1:25 Comment(0)
P
0
library(jsonlite)
library(cat)

jasonRES1 <- jsonlite::toJSON(res1)
cat(jasonRES1)
Pelagianism answered 14/6 at 10:27 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question and how it differs from the previous similar will make it more useful for others in the future. Please consider updating your answer with some explanatory notes. ThanksMope

© 2022 - 2024 — McMap. All rights reserved.