0

I have a dataframe and I want to create individual JSON files and use one of the column names to name each of the JSON files.

The following code will create multiple CSV files, but instead I want JSON. I am using the iris dataset from plyr.

library(plyr)
d_ply(iris, .(Species), function(x) write.csv(x, file = paste(unique(x$Species), ".csv", sep = "")))
3
  • 1
    There are many packages that handle JSON in R: jsonlite, RJSONIO, rjson and others. Commented Dec 21, 2016 at 20:51
  • iris isn't from plyr Commented Dec 21, 2016 at 21:04
  • apply(iris, 2, jsonlite::toJSON) will give you JSON for each column. Commented Dec 21, 2016 at 21:05

1 Answer 1

1

As Nicola suggests, there are many packages for producing JSON. Adapting your d_ply code:

library(jsonlite)
d_ply(iris, .(Species), function(x) {
    writeLines(toJSON(x), file = paste0(x$Species[1], ".json"))
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.