将嵌套数据转换为不带方括号的 json
Convert nested data to json without square brackets
我想上传 ndjson 个包含嵌套数据的文件。
我的代码(见下文)结果为
> {"id":284,"attributes":[{"value":{"T3":1,"T4":0}}]}
> {"id":848,"attributes":[{"value":{"T3":2,"T4":0}}]}
上传此数据导致以下错误:Bad Request","detail":"json: cannot unmarshal array into Go struct field UserData.attributes of type map[string]interface {}","
如果没有方括号,上传会成功,但如果没有用 paste0“手工”编写 json,我似乎无法获得正确的格式,即
> {"id":284,"attributes":{"value":{"T3":1,"T4":0}}}
> {"id":848,"attributes":{"value":{"T3":2,"T4":0}}}
auto_unbox 也没有帮助。有什么建议么?非常感谢
代码
con_out <- file("output.json", open = "wb")
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25)), T4 = 0)
data <- data %>% tidyr::nest(attributes = value)
jsonlite::stream_out(data, con_out)
close(con_out)
编辑:方括号表示列表。但是如果我尝试嵌套数据框,我只能嵌套一层并且“松散” attributes.
con_out <- file("output.json", open = "wb")
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(attribues = data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25))))
jsonlite::stream_out(data, con_out, auto_unbox = T)
close(con_out)
> {"id":284,"value":{"T3":1}}
> {"id":848,"value":{"T3":2}}
列属性包含每行一行的数据框。每个数据框都是一个由方括号组成的行列表。将其转换为列表:
library(jsonlite)
library(tidyverse)
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25)), T4 = 0)
data <- data %>% tidyr::nest(attributes = value)
data %>%
mutate(attributes = attributes %>% map(~ {
.x %>% as.list() %>% map(as.list)
})) %>%
toJSON(pretty = TRUE, auto_unbox = TRUE)
#> [
#> {
#> "id": 284,
#> "attributes": {
#> "value": {
#> "T3": 1,
#> "T4": 0
#> }
#> }
#> },
#> {
#> "id": 848,
#> "attributes": {
#> "value": {
#> "T3": 2,
#> "T4": 0
#> }
#> }
#> }
我想上传 ndjson 个包含嵌套数据的文件。
我的代码(见下文)结果为
> {"id":284,"attributes":[{"value":{"T3":1,"T4":0}}]}
> {"id":848,"attributes":[{"value":{"T3":2,"T4":0}}]}
上传此数据导致以下错误:Bad Request","detail":"json: cannot unmarshal array into Go struct field UserData.attributes of type map[string]interface {}","
如果没有方括号,上传会成功,但如果没有用 paste0“手工”编写 json,我似乎无法获得正确的格式,即
> {"id":284,"attributes":{"value":{"T3":1,"T4":0}}}
> {"id":848,"attributes":{"value":{"T3":2,"T4":0}}}
auto_unbox 也没有帮助。有什么建议么?非常感谢
代码
con_out <- file("output.json", open = "wb")
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25)), T4 = 0)
data <- data %>% tidyr::nest(attributes = value)
jsonlite::stream_out(data, con_out)
close(con_out)
编辑:方括号表示列表。但是如果我尝试嵌套数据框,我只能嵌套一层并且“松散” attributes.
con_out <- file("output.json", open = "wb")
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(attribues = data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25))))
jsonlite::stream_out(data, con_out, auto_unbox = T)
close(con_out)
> {"id":284,"value":{"T3":1}}
> {"id":848,"value":{"T3":2}}
列属性包含每行一行的数据框。每个数据框都是一个由方括号组成的行列表。将其转换为列表:
library(jsonlite)
library(tidyverse)
set.seed(1234)
data <- data.frame(id = sample(1:1000, 10))
data$value <- data.frame(T3 = sample(1:3, 10, replace = T, prob = c(0.5, 0.25, 0.25)), T4 = 0)
data <- data %>% tidyr::nest(attributes = value)
data %>%
mutate(attributes = attributes %>% map(~ {
.x %>% as.list() %>% map(as.list)
})) %>%
toJSON(pretty = TRUE, auto_unbox = TRUE)
#> [
#> {
#> "id": 284,
#> "attributes": {
#> "value": {
#> "T3": 1,
#> "T4": 0
#> }
#> }
#> },
#> {
#> "id": 848,
#> "attributes": {
#> "value": {
#> "T3": 2,
#> "T4": 0
#> }
#> }
#> }