json - Constructing request payload in R using rjson/jsonlite -
my current code seen below attempts construct request payload (body), isn't giving me desired result.
library(df2json) library(rjson) y = rjson::fromjson((df2json::df2json(dataframe))) globalparam = "" req = list( inputs = list( input1 = y ) ,globalparameters = paste("{",globalparam,"}",sep="")#globalparam ) body = enc2utf8((rjson::tojson(req)))
body
turns out
{ "inputs": { "input1": [ { "x": 7, "y": 5, "month": "mar", "day": "fri", "ffmc": 86.2, "dmc": 26.2, "dc": 94.3, "isi": 5.1, "temp": 8.2, "rh": 51, "wind": 6.7, "rain": 0, "area": 0 } ] }, "globalparameters": "{}" }
however, need this:
{ "inputs": { "input1": [ { "x": 7, "y": 5, "month": "mar", "day": "fri", "ffmc": 86.2, "dmc": 26.2, "dc": 94.3, "isi": 5.1, "temp": 8.2, "rh": 51, "wind": 6.7, "rain": 0, "area": 0 } ] }, "globalparameters": {} }
so global parameters have {}, not hardcoded. seemed simple problem, couldn't fix it. please help!
edit:
this dataframe
x y month day ffmc dmc dc isi temp rh wind rain area 1 7 5 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 0.0 0 2 7 4 oct tue 90.6 35.4 669.1 6.7 18.0 33 0.9 0.0 0 3 7 4 oct sat 90.6 43.7 686.9 6.7 14.6 33 1.3 0.0 0 4 8 6 mar fri 91.7 33.3 77.5 9.0 8.3 97 4.0 0.2 0
this example of data frame
> = data.frame("col1" = c(81, 81, 81, 81), "col2" = c(72, 69, 79, 84))
using sample data
dd<-read.table(text=" x y month day ffmc dmc dc isi temp rh wind rain area 1 7 5 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 0.0 0", header=t)
you can do
globalparam = setnames(list(), character(0)) req = list( inputs = list( input1 = dd ) ,globalparameters = globalparam ) body = enc2utf8((rjson::tojson(req)))
note globalparam
looks bit funny because need force named list rjson
treat properly. have when it's empty.
Comments
Post a Comment