# Construct a tbl_json object using a charater string of JSON
json <- '{"animal": "cat", "count": 2}'
json %>% as.tbl_json
# access the "JSON" argument
json %>% as.tbl_json %>% attr("JSON")
# Construct a tbl_json object using multiple documents
json <- c('{"animal": "cat", "count": 2}', '{"animal": "parrot", "count": 1}')
json %>% as.tbl_json
# Construct a tbl_json object from a data.frame with a JSON colum
library(tibble)
farms <- tribble(
~farm, ~animals,
1L, '[{"animal": "pig", "count": 50}, {"animal": "cow", "count": 10}]',
2L, '[{"animal": "chicken", "count": 20}]'
)
farms %>% as.tbl_json(json.column = "animals")
# tidy the farms
farms %>% as.tbl_json(json.column = "animals") %>%
gather_array %>% spread_all
Run the code above in your browser using DataLab