如何使用分组从R dataframe编写JSON对象

时间:2022-10-15 20:05:07

In general I feel there is a need to make JSON objects by folding multiple columns. There is no direct way to do this afaik. Please point it out if there is ..

一般来说,我觉得有必要通过折叠多列来制作JSON对象。没有直接的方法可以做到这一点。如果有..请指出来..

I have data of this from

我有这方面的数据

A B C
1 a x
1 a y
1 c z
2 d p
2 f q
2 f r

How do I write a json which looks like

我怎么写一个看起来像的json

{'query':'1', 'type':[{'name':'a', 'values':[{'value':'x'}, {'value':'y'}]}, {'name':'c', 'values':[{'value':'z'}]}]}

and similarly for 'query':'2'

和“查询”类似:'2'

I am looking to spit them in the mongo import/export individual json lines format. Any pointers are also appreciated..

我期待以mongo导入/导出个人json行格式吐出它们。任何指针也赞赏..

1 个解决方案

#1


1  

You've got a little "non-standard" thing going with two keys of "value" (I don't know if this is legal json), as you can see here:

你有一个“非标准”的东西,有两个“价值”键(我不知道这是不是合法的json),你可以在这里看到:

(js <- jsonlite::fromJSON('{"query":"1", "type":[{"name":"a", "values":[{"value":"x"}, {"value":"y"}]}, {"name":"c", "values":[{"value":"z"}]}]}'))
## $query
## [1] "1"
## 
## $type
##   name values
## 1    a   x, y
## 2    c      z

... with a data.frame cell containing a list of data.frames:

...使用包含data.frames列表的data.frame单元格:

js$type$values[[1]]
##   value
## 1     x
## 2     y
class(js$type$values[[1]])
## [1] "data.frame"

If you can accept your "type" variable containing a vector instead of a named-list, then perhaps the following code will suffice:

如果您可以接受包含向量而不是命名列表的“类型”变量,那么以下代码可能就足够了:

jsonlite::toJSON(lapply(unique(dat[, 'A']), function(a1) {
    list(query = a1, 
         type = lapply(unique(dat[dat$A == a1, 'B']),  function(b2) {
             list(name = b2,
                  values = dat[(dat$A == a1) & (dat$B == b2), 'C'])
         }))
}))
## [{"query":[1],"type":[{"name":["a"],"values":["x","y"]},{"name":["c"],"values":["z"]}]},{"query":[2],"type":[{"name":["d"],"values":["p"]},{"name":["f"],"values":["q","r"]}]}] 

#1


1  

You've got a little "non-standard" thing going with two keys of "value" (I don't know if this is legal json), as you can see here:

你有一个“非标准”的东西,有两个“价值”键(我不知道这是不是合法的json),你可以在这里看到:

(js <- jsonlite::fromJSON('{"query":"1", "type":[{"name":"a", "values":[{"value":"x"}, {"value":"y"}]}, {"name":"c", "values":[{"value":"z"}]}]}'))
## $query
## [1] "1"
## 
## $type
##   name values
## 1    a   x, y
## 2    c      z

... with a data.frame cell containing a list of data.frames:

...使用包含data.frames列表的data.frame单元格:

js$type$values[[1]]
##   value
## 1     x
## 2     y
class(js$type$values[[1]])
## [1] "data.frame"

If you can accept your "type" variable containing a vector instead of a named-list, then perhaps the following code will suffice:

如果您可以接受包含向量而不是命名列表的“类型”变量,那么以下代码可能就足够了:

jsonlite::toJSON(lapply(unique(dat[, 'A']), function(a1) {
    list(query = a1, 
         type = lapply(unique(dat[dat$A == a1, 'B']),  function(b2) {
             list(name = b2,
                  values = dat[(dat$A == a1) & (dat$B == b2), 'C'])
         }))
}))
## [{"query":[1],"type":[{"name":["a"],"values":["x","y"]},{"name":["c"],"values":["z"]}]},{"query":[2],"type":[{"name":["d"],"values":["p"]},{"name":["f"],"values":["q","r"]}]}]