For R models, data parsing is done with the package rjson. Prior to model.predict being called, the data is parsed and transformed into an R object.
This means that an R object is actually being passed to model.predict.
Generally, data is processed in the following sequence:
- API request
- data.frame(jsonlite::fromJSON(data),stringsAsFactors=TRUE)
- model.predict(data)
- rjson::toJSON()
- response
In Step 2, jsonlite can be changed to use rjson by changing the JSONLITE environment variable to equal FALSE in the model's Advanced tab within the Promote web application.
# below (for examples sake) is incoming JSON parsed BEFORE it gets sent to model.predict()
#API Requests --> model.predict(rjson::fromJSON(df))
model.predict <- function(df) {
lg <- df
m <- lapply(lg[[1]]$users, function(x) c(paste(x$user['name'],sample(1:2,1)), x$user['user_id'], x['ts']))
m <- do.call(rbind, m)
# below, we split the dataframe so it is returned as JSON arrays
m <- unname(split(m, 1:nrow(m)))
m
}
# rjson::toJSON() --> API Response
Local test
To locally test JSON data, parsing must be done in the Promote model.