Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Nest() 1.0.0. and subsequent jsonlite::toJSON #750

Closed
skr5k opened this issue Sep 18, 2019 · 3 comments
Closed

Problem with Nest() 1.0.0. and subsequent jsonlite::toJSON #750

skr5k opened this issue Sep 18, 2019 · 3 comments

Comments

@skr5k
Copy link

skr5k commented Sep 18, 2019

Hey,

I use nest() a lot for creating JSONs from dataframes to interact with an API. Here's an example:

require(tidyverse)
require(lubridate)
require(tidyr)
require(jsonlite)

df = data.frame(id = "xyz",
                time =  seq(from = ymd_hms("2020-10-10 00:00:00", tz = "GMT"),
                            to = ymd_hms("2020-10-10 23:00:00", tz = "GMT"),
                            by="hours") %>% as.integer(),
                value = 1:24)

df %>% nest_legacy(time, value) %>% toJSON(pretty=FALSE, auto_unbox = FALSE)

This returns:

[{"id":"xyz","data":[{"time":1602288000,"value":1},{"time":1602291600,"value":2},{"time":1602295200,"value":3},{"time":1602298800,"value":4},{"time":1602302400,"value":5},{"time":1602306000,"value":6},{"time":1602309600,"value":7},{"time":1602313200,"value":8},{"time":1602316800,"value":9},{"time":1602320400,"value":10},{"time":1602324000,"value":11},{"time":1602327600,"value":12},{"time":1602331200,"value":13},{"time":1602334800,"value":14},{"time":1602338400,"value":15},{"time":1602342000,"value":16},{"time":1602345600,"value":17},{"time":1602349200,"value":18},{"time":1602352800,"value":19},{"time":1602356400,"value":20},{"time":1602360000,"value":21},{"time":1602363600,"value":22},{"time":1602367200,"value":23},{"time":1602370800,"value":24}]}] 

When I run instead:

df %>% nest(data = c(time, value)) %>% toJSON(pretty=FALSE, auto_unbox = FALSE)

I get an error saying No method asJSON S3 class: vctrs_vctr.

It seems to me that the new nest creates an object of class vctrs_vctr (which I dont know) inside the data.frame instead of using a list as a container for the nested elements. In case I got that right, can I make nest create a list or alternatively, how can I have the nest() function output something that's digestable for toJSON?

Thanks a lot in advance!
skr5k

@hadley
Copy link
Member

hadley commented Sep 18, 2019

Same as jeroen/jsonlite#292 (although it's not clear where this will need to be fixed yet)

@SymbolixAU
Copy link

A work-around for jsonlite is to use force = TRUE when it doesn't recognise the class

df %>% 
  nest(data = c(time, value)) %>% 
  toJSON(pretty=FALSE, auto_unbox = FALSE, force = TRUE)

# [{"id":"xyz","data":[{"time":1602288000,"value":1} ... etc

And also jsonify works too

df %>% 
  nest(data = c(time, value)) %>% 
  jsonify::to_json( unbox = T )

# [{"id":"xyz","data":[{"time":1602288000,"value":1} ... etc

@hadley
Copy link
Member

hadley commented Nov 13, 2019

Should now be resolved in dev tidyr via changes to dev vctrs.

@hadley hadley closed this as completed Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants