rjsontidyverse

Generate JSON from R data, tidyverse style


Is there a way to generate a JSON string from data that is contained in an R Tidyverse tibble such like this:

library(tidyverse)
d <- tribble(
  ~last_name,  ~first_name, ~role,          ~admin,
  "Gale",      "Dorothy",   "board member", FALSE,
  "Man",       "Tin",       "member",       TRUE
)

d |>
  mutate(json_data = generate_some_json(role, admin)) |> 
  select(last_name, first_name, json_data)
#> Error in `mutate()` at dplyr/R/select.R:54:3:
#> ℹ In argument: `json_data = generate_some_json(role, admin)`.
#> Caused by error in `generate_some_json()`:
#> ! could not find function "generate_some_json"

Created on 2024-10-16 with reprex v2.1.1

Where generate_some_json is the function that I am looking for, but which may not even exist?

Desired, but fabricated result:

# A tibble: 2 x 3
  last_name first_name json_data
  <chr>     <chr>      <chr>        
1 Gale      Dorothy    {"role": "board member", "admin": "FALSE"}
2 Man       Tin        {"role": "member", "admin": "TRUE"} 

I've experimented with the jsonlite and tidyjson packages but either wasn't doing it right or (what I suspect) these libraries do not support this kind of operation.

I do get the desired result with a custom function (below), but I would prefer to have something more sustainable for future tasks (e.g., with a larger number of variables).

make_json <- function(role, admin) {
  str_glue(
    '{{',
    '"role": "{role}"',
    ', ',
    '"admin": "{admin}"',
    '}}'
  )
}
# and then use something like
# mutate(json_data = make_json(role, admin))

Created on 2024-10-16 with reprex v2.1.1


Solution

  • d %>%
       group_by(last_name, first_name) %>%
       summarise(json_data = jsonlite::toJSON(pick(everything())), .groups = 'drop')
    
    # A tibble: 2 × 3
    # Groups:   last_name [2]
      last_name first_name json_data                                      
      <chr>     <chr>      <json>                                         
    1 Gale      Dorothy    "[{\"role\":\"board member\",\"admin\":false}]"
    2 Man       Tin        "[{\"role\":\"member\",\"admin\":true}]"