community
cancel
Showing results for 
Search instead for 
Did you mean: 

Alteryx Promote Knowledge Base

Definitive answers from Promote experts.

Parsing JSON in R Promote Models: jsonlite vs. rjson

Sr. Data Science Content Engineer
Sr. Data Science Content Engineer
Created on

When making a call to a Promote model, the input data used to make a prediction is sent in a JSON format. When working with an R model, prior to reaching the model.predict() function, the JSON string that was sent to your model is converted to an R format (either an R dataframe or an R list). By default, this conversion is performed with the fromJSON() function in the jsonlite R package. 

 

In Promote, you do have the option of changing the package that is used to perform this conversion from jsonlite to rjson by adding the environment variable JSONLITE = FALSE to your model. 

 

You may want to change which package is used to convert your incoming data because jsonlite::fromJSON and rjson::fromJSON will behave differently based on the format of the incoming JSON data.

 

For example, with the input data (in JSON form):

 

'[{"name": "miss"},{"name": "piggy"}]'

jsonlite::fromJSON will return a data frame:

 

    name
1  miss
2  piggy

 where rjson::fromJSON will return a list of two lists:

 

[[1]]
[[1]]$name
[1] "miss"


[[2]]
[[2]]$name
[1] "piggy"

 

When you convert the output of rjson::fromJSON to a dataframe using data.frame(), it looks like this:

 

    name name.1
1   miss piggy

 

You can test which package and function will be most appropriate for your use case by converting your JSON input in R using each function:

 

json <- data #this is your test input data in JSON form

#test rjson
data.frame(rjson::fromJSON(json),stringsAsFactors = TRUE)

#test jsonlite
data.frame(jsonlite::fromJSON(json),stringsAsFactors = TRUE)

 

Please note that the model.predict() must function must be supplied a dataframe, and not a list. The structure of the input dataframe can be specified by you in your code.

 

Please also note that the output of your model will be converted back to a JSON string using the toJSON function in jsonlite, regardless of which package you have configured your model to use to perform the conversion.

 

If you find that you would prefer to use rjson for your model instead of jsonlite, you can easily make this change by adding the Environment Variable: JSONLITE = FALSE.

 

To add environment variables to a model, first, open your model dashboard by clicking on the model’s name in Promote.

 

openmodel.png

 

After opening the dashboard, navigate to the Advanced tab, and find the row for environment variables. Click on the small plus sign in this row to add an environment variable.

 

addEnvVariable.png

 

 

To switch to rjson, in the window that pops up, add a variable where the Name is JSONLITE and the Value is FALSE, and click SAVE.

 

 

2018-10-02_13-45-17.png

 

 

You should see your new environment variable populated in the Environment Variables row of the Advanced tab for your model.

 

2018-10-02_13-47-32.png

 

There you have it! Next time you execute a prediction for this model, your inputs will be converted with rjson::fromJSON() instead of jsonlite::fromJSON()!

 

For further reading on the differences between jsonlite and rjson, please see the following resources:

 

A biased comparison of JSON packages in R

Better handling of JSON data in R?

 

Other Environment Variables

 

There is currently one other built-in environment variable in Promote called PREDICTION_TIMEOUT, which lets you modify the amount of time Promote will wait before timing out on a prediction. For more information, please see this Community Knowledge Base article.