Creating JSON format data through data frames in R -
i want create json data of form
{"recipe name": "abc", "main ingredient": "xyz", "ingredients": {type:"a" id:"1"}, {type:"b" id:"2"}, {type:"b" id:"3"} "servings": 3,}
i have data frame of type:
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 ....
i have tried usign rjson package got character(0) output .can me please ?
i did find 1 method though
>library(rjsonio) >tojson(dataframe)
here output:
[1] "{\n \"factor1\": [ \"115g\", \"1\", null, null ],\n\"unit1\": [ \"tub\", \"cups\", \"greek\", \"baby\" ],\n\"item1\": [ \"tomatoes na\", \"kalamata olives\", \"feta cheese\", \"rocket na\" ] \n}"
which not in required format
your desired output isn't correctly structured: missing commas (minor), , ingredients
needs explicit list (using [
, ]
, nothing implicit allowed). also, r doesn't enjoy spaces in column names, removed them. restructured output:
{"recipename": "abc", "mainingredient": "xyz", "ingredients": [{"type":"a", "id":"1"}, {"type":"b", "id":"2"}, {"type":"b", "id":"3"}], "servings": 3}
since data.frame example bit difficult read/parse, here 1 similar enough, think:
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
from here, takes little effort convert nested lists within ingredients
more string:
datlist <- as.list(dat) datlist$ingredients <- mapply(function(a,b) { a1 <- strsplit(substring(a, 2, nchar(a)-1), ",")[[1]] ## might want remove leading/trailing quotes 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 # remove now-unnecessary field library(rjsonio) # jsonlite::tojson looks similar, not 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 ## ] ## }>
unfortunately, taking single row data.frame results in nested components (specifically ingredients
), not adhere desired output:
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 ## }
Comments
Post a Comment