通过R中的数据帧创建JSON格式数据

时间:2015-07-01 20:00:32

标签: json r

我想创建一个

形式的JSON数据
{"Recipe Name": "ABC",
"Main Ingredient": "xyz",
"Ingredients": {type:"a"
                 id:"1"},
               {type:"b"
                 id:"2"},
                {type:"b"
                 id:"3"}
"Servings": 3,}

我有一个类型为

的数据框
Recipe, Recipe Id ,Ingredients,Ingredients ID,Servings ,Main Ingredient,Main Ingredient ID  
 "abc"  , 2     ,    {"a","b","c"}  , {1,2,3,}   , 5   , "f"   ,7   
  "bcf"  , 3   ,       {"d","e","f"} , {4,5,7}  ,  4    ,"g"   ,8
   ....

我已尝试使用rjson包但输入了字符(0)。任何人都可以帮我这个吗?

我确实找到了一种方法

>library(RJSONIO)
>toJSON(dataframe)

这是输出:

  

[1]“{\ n \”factor1 \“:[\”115g \“,\”1 \“,null,null],\ n \”unit1 \“:[\”tub \“,\ “杯子”,“希腊”,“宝贝”,\ n \“\ item1 \”:[\“西红柿NA \”,“卡拉马塔橄榄”,“奶酪”,“火箭NA \“] \ n}”

不符合要求的格式

1 个答案:

答案 0 :(得分:0)

您想要的输出结构不正确:您缺少一些逗号(次要),而您的Ingredients需要是明确的列表(使用[],没有暗示允许)。此外, R 并不真正享受列名称中的空格,因此我删除了它们。重组后的输出:

{"RecipeName": "ABC",
 "MainIngredient": "xyz",
 "Ingredients": [{"type":"a", "id":"1"},
                 {"type":"b", "id":"2"},
                 {"type":"b", "id":"3"}],
 "Servings": 3}

由于你的data.frame示例有点难以阅读/解析,我认为这是一个足够相似的例子:

dat <- data.frame(
    RecipeName=c('ABC', 'DEF'),
    MainIngredient=c('xyz', 'lmn'),
    Ingredients=c('{"a","b","c"}', '{"d","e","f"}'),
    IngredientsId=c('{1,2,3}', '{4,5,6}'),
    Servings=c(7,8),
    stringsAsFactors=FALSE
)
dat
##   RecipeName MainIngredient   Ingredients IngredientsId Servings
## 1        ABC            xyz {"a","b","c"}       {1,2,3}        7
## 2        DEF            lmn {"d","e","f"}       {4,5,6}        8

从这里开始,将Ingredients中的嵌套列表转换为不仅仅是字符串的内容需要花费一些精力:

datlist <- as.list(dat)
datlist$Ingredients <- mapply(function(a,b) {
    a1 <- strsplit(substring(a, 2, nchar(a)-1), ",")[[1]]
    ## might want to remove leading/trailing quotes as well
    a1 <- gsub('"', '', a1)
    b1 <- strsplit(substring(b, 2, nchar(b)-1), ",")[[1]]
    mapply(function(e,f) list(type=e, id=f), a1, b1, SIMPLIFY=FALSE, USE.NAMES=FALSE)
}, datlist$Ingredients, datlist$IngredientsId,
    SIMPLIFY=FALSE, USE.NAMES=FALSE)
datlist$IngredientsId <- NULL # to remove the now-unnecessary field

library(RJSONIO) # jsonlite::toJSON looks similar, not exactly the same
cat(toJSON(datlist, pretty=T))
## {
##  "RecipeName" : [
##      "ABC",
##      "DEF"
##  ],
##  "MainIngredient" : [
##      "xyz",
##      "lmn"
##  ],
##  "Ingredients" : [
##      [
##          {
##              "type" : "a",
##              "id" : "1"
##          },
##          {
##              "type" : "b",
##              "id" : "2"
##          },
##          {
##              "type" : "c",
##              "id" : "3"
##          }
##      ],
##      [
##          {
##              "type" : "d",
##              "id" : "4"
##          },
##          {
##              "type" : "e",
##              "id" : "5"
##          },
##          {
##              "type" : "f",
##              "id" : "6"
##          }
##      ]
##  ],
##  "Servings" : [
##      7,
##      8
##  ]
## }> 

不幸的是,从data.frame中获取一行会导致嵌套组件(特别是Ingredients),因此它不能完美地符合您所需的输出:

datlist2 <- as.list(dat[1,])
## rest of mapply(mapply(...)) ...
cat(toJSON(datlist2, pretty=TRUE))
## {
##  "RecipeName" : "ABC",
##  "MainIngredient" : "xyz",
##  "Ingredients" : [
##      [
##          {
##              "type" : "a",
##              "id" : "1"
##          },
##          {
##              "type" : "b",
##              "id" : "2"
##          },
##          {
##              "type" : "c",
##              "id" : "3"
##          }
##      ]
##  ],
##  "Servings" : 7
## }