3

Using jsonlite package I am able to write a dataframe to json e.g.

library(jsonlite)
library(tidyverse)

mtcars %>% toJSON() %>% write('data/mtcars.json')
diamonds %>% toJSON() %>% write('data/diamonds.json')

My question is, is it possible to add both mtcars and diamonds to the same json object?

1 Answer 1

4

Yes: put the datasets in a list before serializing to JSON:

library(jsonlite)

datasets <- list(datasets = list(mtcars = mtcars, iris = iris))
write(toJSON(datasets), "datasets.json")

This will result in a JSON file with a structure like:

{
  "datasets": {
    "mtcars": [...],
    "iris": [...]
  }
} 
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.