1

I have a list my_list <- list(name="Fred", age="5")

I expected my_list %>% toJSON to return {"name": "Fred", "age": "5"}

Instead, it returns {"name":["Fred"],"age":["5"]}

How can I convert this list to JSON whilst avoiding the square brackets?

stevec
  • 41,291
  • 27
  • 223
  • 311

2 Answers2

6

We can use the auto_unbox which is by default FALSE

library(dplyr)
library(jsonlite)
my_list %>% 
   toJSON(auto_unbox = TRUE)
#{"name":"Fred","age":"5"} 
akrun
  • 874,273
  • 37
  • 540
  • 662
4

Use auto_unbox (the default is FALSE)

toJSON(my_list, auto_unbox =T)
camille
  • 16,432
  • 18
  • 38
  • 60
Grada Gukovic
  • 1,228
  • 7
  • 13