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

Serialization to JSON does not transfer attributes (like names) #24

Open
frankcorneliusmartin opened this issue Jan 24, 2024 · 0 comments

Comments

@frankcorneliusmartin
Copy link
Member

The jsonlite::toJSON(...) removes the names attribute from vectors. See why that is here: jeroen/jsonlite#76 We use this in quite a few algorithms. A temporary work around would be to add keep_vec_names=TRUE to the .toJSON function. Unfortunately they are going to drop this argument:

Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

A better solution would be to not use named vectors but named lists:

# named vector
vec <- c("a"=1, "b"=2)
jsonlite::toJSON(vec)
# output: [1,2] 

# named list
lis <- list("a"=1, "b"=2)
jsonlite::toJSON(lis)
# output: {"a":[1],"b":[2]} 

Best solution is to covert your vectors before transport (as.list):

vec <- c("a"=1, "b"=2)
jsonlite::toJSON(as.list(vec))
# output: [1,2] 
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

1 participant